Question enthuware.ocajp.i.v8.2.915
Moderator: admin
-
- Posts: 3
- Joined: Sun Feb 05, 2017 11:39 am
- Contact:
Question enthuware.ocajp.i.v8.2.915
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(); ?
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(); ?
-
- Posts: 6
- Joined: Sun Feb 05, 2017 3:42 pm
- Contact:
Re: Question enthuware.ocajp.i.v8.2.915
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:
would only be allowed if you had imported the Logger class. Like so:
or alternatively importing all classes in the util.log4j package:
(Someone please correct me if I made a mistake somewhere! Ty!)
The way you suggest:
Code: Select all
log4j.Logger logger = new log4j.Logger();
Code: Select all
import util.log4j.Logger;
Code: Select all
import util.log4j.*;
-
- Site Admin
- Posts: 9791
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: Question enthuware.ocajp.i.v8.2.915
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 28
- Joined: Sun Oct 25, 2015 10:14 am
- Contact:
Re: Question enthuware.ocajp.i.v8.2.915
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.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.
-
- Site Admin
- Posts: 9791
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: Question enthuware.ocajp.i.v8.2.915
Not sure what you mean. Can you write a piece of code to illustrate what you mean?
If you like our products and services, please help us by posting your review here.
-
- Posts: 28
- Joined: Sun Oct 25, 2015 10:14 am
- Contact:
Re: Question enthuware.ocajp.i.v8.2.915
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
}
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
}
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!?
-
- Site Admin
- Posts: 9791
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: Question enthuware.ocajp.i.v8.2.915
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.
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 85
- Joined: Mon Dec 24, 2018 6:24 pm
- Contact:
Re: Question enthuware.ocajp.i.v8.2.915
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-------
package------
subdirectory-------
subsubdirectory-------
-
- Site Admin
- Posts: 9791
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: Question enthuware.ocajp.i.v8.2.915
No.
If you like our products and services, please help us by posting your review here.
-
- Posts: 85
- Joined: Mon Dec 24, 2018 6:24 pm
- Contact:
Re: Question enthuware.ocajp.i.v8.2.915
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.
-
- Site Admin
- Posts: 9791
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: Question enthuware.ocajp.i.v8.2.915
Yes, you have to import the whole package name.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 3 guests