Question enthuware.ocajp.i.v8.2.915

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
edward109
Posts: 3
Joined: Sun Feb 05, 2017 11:39 am
Contact:

Question enthuware.ocajp.i.v8.2.915

Post 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(); ?

java_learner
Posts: 6
Joined: Sun Feb 05, 2017 3:42 pm
Contact:

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

Post 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!)

admin
Site Admin
Posts: 10018
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

Kevin30
Posts: 28
Joined: Sun Oct 25, 2015 10:14 am
Contact:

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

Post 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.

admin
Site Admin
Posts: 10018
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

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.

Kevin30
Posts: 28
Joined: Sun Oct 25, 2015 10:14 am
Contact:

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

Post 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!?

admin
Site Admin
Posts: 10018
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

crazymind
Posts: 85
Joined: Mon Dec 24, 2018 6:24 pm
Contact:

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

Post 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-------

admin
Site Admin
Posts: 10018
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

crazymind
Posts: 85
Joined: Mon Dec 24, 2018 6:24 pm
Contact:

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

Post 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.

admin
Site Admin
Posts: 10018
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

Yes, you have to import the whole package name.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 20 guests