Page 1 of 1

Question enthuware.ocajp.i.v8.2.915

Posted: Sun Feb 05, 2017 4:32 pm
by edward109
package util.log4j;

package util;
Do I still have to specify util while my starting point is util ?

util.log4j.Logger logger = new util.log4j.Logger();

why not
log4j.Logger logger = new log4j.Logger(); ?

Re: Question enthuware.ocajp.i.v8.2.915

Posted: Sun Feb 05, 2017 5:15 pm
by java_learner
If you don't import the class, then you need to provide the fully qualified name in order for the compiler to find the definition.

The way you suggest:

Code: Select all

log4j.Logger logger = new log4j.Logger();
would only be allowed if you had imported the Logger class. Like so:

Code: Select all

import util.log4j.Logger;
or alternatively importing all classes in the util.log4j package:

Code: Select all

import util.log4j.*;
(Someone please correct me if I made a mistake somewhere! Ty!)

Re: Question enthuware.ocajp.i.v8.2.915

Posted: Sun Feb 05, 2017 10:42 pm
by admin
Java doesn't allow partial package name import. For example, if your class is in package a.b.c, you cannot do import a.b and then use c.MyClass in the code. You have to use a.b.c.MyClass. Or import the package a.b.c and then use MyClass directly.

Re: Question enthuware.ocajp.i.v8.2.915

Posted: Thu Aug 30, 2018 8:13 am
by Kevin30
Java doesn't allow partial package name import. For example, if your class is in package a.b.c, you cannot do import a.b and then use c.MyClass in the code. You have to use a.b.c.MyClass. Or import the package a.b.c and then use MyClass directly.
That is correct. But if you ARE in package a.b, you can then subsequently import package c and then use myClass in the code, if I am not mistaken.

Re: Question enthuware.ocajp.i.v8.2.915

Posted: Thu Aug 30, 2018 8:22 am
by admin
Not sure what you mean. Can you write a piece of code to illustrate what you mean?

Re: Question enthuware.ocajp.i.v8.2.915

Posted: Tue Sep 04, 2018 3:17 pm
by Kevin30
admin wrote:
Thu Aug 30, 2018 8:22 am
Not sure what you mean. Can you write a piece of code to illustrate what you mean?
Mmmm. I tried, but apparently I can't do that (with the IDE at least, maybe with the command prompt it's possible but I'm not really proficient enough with the command prompt to do that).

I know that a subpackage isn't part of the package, but I also know that if you ARE IN that package, you can access that subpackage by using the fully qualified name (if the accessmodifier is public), like so:

Code: Select all

package a.b.c;
public class myClass {
    public int x = 5;
}

package a.b;
public class AB {
    public a.b.c.myClass x;    // you can access variable x in class myClass by using the fully qualified name
}
So I thought that it follows that if you are in package a.b; you can then import package c and then use myClass in the code, like so:

Code: Select all

package a.b.c;
public class myClass {
    public int x = 5;
}

package a.b;
import c.*;
public class AB {
    public myClass x;		// I thought you could also access variable x in class myClass by importing package c
}
but it appears that isn't possible (in the IDE at least).

So I guess that just underscores the point you were making that Java doesn't allow partial package name import.

So the conclusion must be then that it just isn't possible to access a variable from a subpackage by anything other than EITHER the fully qualified name OR an full import statement (with or without wildcard) of that subpackage, but not a mix of both I guess!?

Re: Question enthuware.ocajp.i.v8.2.915

Posted: Tue Sep 04, 2018 10:32 pm
by admin
OK, I now understand what you mean by "when you are in a package". You mean the class in which you are accessing some other class belongs to that package. No, it is not possible to import partial package. So if you class AB is in package a.b, and in this class if you want to access another class MyClass of package a.b.c, you cannot just import c.*. You can either use a.b.c.MyClass (which works irrespective of whether you use an import statement or not) or use import a.b.c.MyClass (or import a.b.c.*).

BTW, we recommend using command line (not any IDE) while preparing for the exam. IDEs do a lot of things in the background (such a code highlighting, bracket matching, import organization, code warning etc.), without the user having to do anything. This creates a false impression that "java" is doing all those things. So better stick with the command line at least for the exam preparation.

Paul.

Re: Question enthuware.ocajp.i.v8.2.915

Posted: Sat Jan 05, 2019 2:20 pm
by crazymind
Hi, Is log4j a subdirectory of util package? Let's say I have a directory called subsubdirectory which locates in the subdirectory. Will subsubdirectory be imported if I do package.subdirectory.* ?

package------
subdirectory-------
subsubdirectory-------

Re: Question enthuware.ocajp.i.v8.2.915

Posted: Sat Jan 05, 2019 7:29 pm
by admin
crazymind wrote:
Sat Jan 05, 2019 2:20 pm
Let's say I have a directory called subsubdirectory which locates in the subdirectory. Will subsubdirectory be imported if I do package.subdirectory.* ?
No.

Re: Question enthuware.ocajp.i.v8.2.915

Posted: Sun Jan 06, 2019 5:00 pm
by crazymind
Thanks, so it only import all the classes in that subdirectory. I have to do subdirectory.subsubdirectory.* if I want to import all the classes in subsubdirectory.

Re: Question enthuware.ocajp.i.v8.2.915

Posted: Sun Jan 06, 2019 10:05 pm
by admin
Yes, you have to import the whole package name.