Page 1 of 1

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

Posted: Fri Jan 10, 2014 9:10 pm
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.

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

Posted: Fri Jan 10, 2014 9:56 pm
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.

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

Posted: Wed Sep 10, 2014 3:07 am
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).

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

Posted: Wed Nov 05, 2014 6:19 pm
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.

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

Posted: Thu Nov 06, 2014 12:30 am
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.

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

Posted: Sat Nov 30, 2019 3:23 am
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?

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

Posted: Sat Nov 30, 2019 3:30 am
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.

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

Posted: Sun Jun 28, 2020 2:20 pm
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?

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

Posted: Mon Jun 29, 2020 2:42 am
by admin
Right, classes in non-default package can't access classes in the default package.

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

Posted: Wed Jan 19, 2022 5:29 pm
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.