About Question enthuware.ocajp.i.v7.2.894 :

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

Moderator: admin

Post Reply
thupten
Posts: 4
Joined: Thu Jan 09, 2014 3:21 pm
Contact:

About Question enthuware.ocajp.i.v7.2.894 :

Post by thupten »

The question asks. Which packages are automatically imported.

The 2 correct answers were (marked below with [x])
java.util
System
java.lang [x]
java.io
String
The package with no name [x]

is the last answer correct? is a package with no name imported?
thanks.

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

Re: About Question enthuware.ocajp.i.v7.2.894 :

Post by admin »

Yes, it is correct for the given code because the given class itself belongs to the same package (i.e. the default/no-name package).


However, this default package cannot be imported in classes that belong to any other package at all, not even with any sort of import statement. So for example, if have a class named SomeClass in package test, you cannot cannot access TestClass defined in the problem statement (as it is defined in the default package) at all because there is no way to import it.

As per JLS Section 7.5:
A type in an unnamed package has no canonical name, so the requirement for a canonical name in every kind of import declaration implies that (a) types in an unnamed package cannot be imported, and (b) static members of types in an unnamed package cannot be imported.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

thchuong
Posts: 8
Joined: Wed Sep 10, 2014 2:42 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.894 :

Post by thchuong »

admin's explanation is a little complicated to me. I found a sentence in oracle tutorial that perfectly describe this issue:
http://docs.oracle.com/javase/tutorial/ ... epkgs.html
For convenience, the Java compiler automatically imports two entire packages for each source file: (1) the java.lang package and (2) the current package (the package for the current file).

guitarmenace
Posts: 1
Joined: Wed Nov 05, 2014 5:54 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.894 :

Post by guitarmenace »

Answer Explanation: If there is no package statement in the source file, the class is assumed to be created in a default package that has no name. In this case, all the types created in this default package will be available to this class without any import statement.

This would seem to imply that members of the unnamed package are automatically imported not automatically accessible. While it may be a matter of semantics, it seems inaccurate and could potentially undermine the concept of default class accessibility with respect to other members of it's package. For example...

given class Apple and class Orange defined in com.enthuware.packages as com.enthuware.packages.Apple and com.enthuware.packages.Orange would you ever say that class Apple can refer to other members of the same package by their simple names because it automatically imports all the members of the com.enthuware.packages package? Or is it really because they are in the same package that they need not be imported at all?

From JLS 7.3, 7.4.2, JLS 7.5,

members can't and aren't imported from an unnamed package.

Members of an unnamed package are accessible to a class with no package statement because they are in the same unnamed package not because the members are automatically imported.

Also

I was wondering about the option in the exam question for java.io...

The Java Language Specification says

"All the compilation units of the predefined package java and its subpackages lang and io are always observable."

I was wondering how it is possible to invoke the method println() on System.out without having to explicitly import java.io if java.io is not automatically imported. For example...

System.out.println("Hello World");

without...

import java.io.PrintStream

Thank you for your time.

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

Re: About Question enthuware.ocajp.i.v7.2.894 :

Post by admin »

The whole purpose of import statement is to create a shortcut while writing code. Instead of writing x.y.z.A, an import allows you to write A. That is all. So the concept of "import" applies only for classes that belong in a different package that the class where you are trying to access them from. There is no need to "import" the classes if they are already in the same package.

Now, the JLS clearly says in 7.5 that the default package cannot be "imported". This means "cannot be imported" in other packages because importing in the same package doesn't make sense anyway.

Regarding, System.out.println("Hello World"):
A package needs to be imported only if you are referring to that a class belonging to that package in your class. You are not referring to PrintStream class in your code. The System class refers to PrintStream class in its code while defining the variable out. So System class needs to import java.io package (or use the full name of the class) and not your class.
If you like our products and services, please help us by posting your review here.

MarcATC
Posts: 3
Joined: Wed Oct 10, 2018 4:57 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.894 :

Post by MarcATC »

I got confused because, as I understand, package members are imported and not whole packages.
So (IMHO) the question should be:
"The following are the complete contents of TestClass.java file. Members of which packages are automatically imported?

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

Re: About Question enthuware.ocajp.i.v7.2.894 :

Post by admin »

Yes, that would be more technically more precise but "importing packages" means the same thing. There is no distinction between the two. It is common and also valid to say that you are importing a package rather than saying you are importing members of a package because there is nothing like importing a package in the sense that you are referring to.
If you like our products and services, please help us by posting your review here.

Sieusc
Posts: 21
Joined: Mon Mar 02, 2020 3:38 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.894 :

Post by Sieusc »

So for example, if you have a class named SomeClass in package test, you cannot access TestClass defined in the problem statement (as it belongs to the unnamed package) at all because there is no way to import it.
Does this mean that public classes in the default package can't be accessed at all by classes in other packages? I suppose one would normally use FQN but that is not applicable and possible with default package?

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

Re: About Question enthuware.ocajp.i.v7.2.894 :

Post by admin »

Right, classes in non-default package can't access classes in the default package.
If you like our products and services, please help us by posting your review here.

tangjm
Posts: 5
Joined: Mon Jan 17, 2022 12:32 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.894 :

Post by tangjm »

MarcATC wrote:
Sat Nov 30, 2019 3:23 am
I got confused because, as I understand, package members are imported and not whole packages.
So (IMHO) the question should be:
"The following are the complete contents of TestClass.java file. Members of which packages are automatically imported?
Agreed. I also thought the question could have been better phrased. While the TestClass.java file is part of the unnamed package, it makes little sense to say that 'the package with no name' is automatically imported. The use of the word 'imported' here seems to also go against the language used in Section 7.5 of the JLS. Maybe something along the lines of 'Which of these options are implicitly part of this file?' would account for how the unnamed package is implicitly "declared" rather than imported.

Post Reply

Who is online

Users browsing this forum: No registered users and 41 guests