Hi all. I am using the Enthuware 1z0-815 practice test to prepare for the OCP Java 11 exam (1z0-815). I have some questions regarding question #14 on the first test.
The question asks which packages are automatically imported by a Java file which has no package statement. The answer given is that java.lang and the unnamed package are automatically imported. The explanation is
If there is no package statement in the source file, the class is assumed to be created in an unnamed package that has no name. In this case, all the types created in this package will be available to this class without any import statement.
I am a little confused by this. While this is true, to my mind the fact that all of the types are available does not necessarily mean that the unnamed package is imported. The most relevant info I have found so far is from the JLS (Java Language Specification), at the beginning of Chapter 7.
Code in a compilation unit automatically has access to all types declared in its package and also automatically imports all of the public types declared in the predefined package java.lang .
Code in named packages has access to all types declared in its package too. I thought that since the source file is part of the unnamed package, it automatically has access to all of the types declared in the unnamed package, and therefore would not have to import it. I also performed a search for the unnamed package in the JLS 11 document, and I couldn’t find anything about it being imported.
My question: does a compilation unit with no package statement import the unnamed package? Also, more generally: does a compilation unit import its own package implicitly?
That quote is the explanation of the answer; it is the answer which I was wondering about. The question asks which packages are imported, and the answer is java.lang and the package with no name (unnamed package).
I see what you're saying now. On one hand, I doubt there's a different mechanism the compiler uses to automatically put java.lang classes and those in the unnamed package in scope so if java.lang is "automatically imported" then by the same token, it's not a stretch to reason that classes in the unnamed packages are also treated the same way. On the other hand, if you're really going to be a stickler for semantics and the JLS doesn't say that classes in the unnamed package are automatically imported but still somehow are accessible, then you have an argument that the statement "unnamed packages are automatically imported" is subject to debate.
I wouldn't worry about it too much because I don't think the exam is going to be that nit-picky, if that's your concern. All that matters is that you understand that java.lang.* classes and those in the unnamed package will be accessible to a class that doesn't have a package declaration.
The best ideas are the crazy ones. If you have a crazy idea and it works, it's really valuable.—Kent Beck
You know how pedantic I can be; that tells me that no classes in the current package (named or unnamed) are imported because they don't need to be imported, and there is an implicit import of everything public in the java.base/java.lang package. Even if you don't write import java.lang.String; But I think Junilu is right: the exam will be written so there is no ambiguity so I don't think this semantic pedantry will make any difference to your exam.
Villains always have antidotes. They're funny that way. Here's an antidote disquised as a tiny ad: