From http://cs-fundamentals.com/tech-interview/java/which-java-package-is-imported-by-default.php
In every Java program there can be one unnamed package, which is simply a package with no name. If you omit the package statement while writing the class definition, the class name is placed into the default package, which has no name. Java compiler automatically imports this package.
Is that true?
EDIT
My concerning about Java compiler automatically imports this package.
No, the unnamed package is not imported by the compiler.
Please note the following from the Java Language Specification.
From Compilation Units:
From Packages:
From Observability of a Package:
Note that the unnamed package is not listed.
From Import Declarations:
No. The statement
is not correct in several respects.
First of all, packages cannot be imported. §7.5 (JLS) says:
This refers to named types and static members, only. Not packages! Although ... there is a convenient method to import every named type from a package with
import my.pkg.*;
.Second, the compiler will not automaticaly import anything from the unnamed package. In fact, it is not possible to refer to any program element of the unnamed package from any named package.
I guess that this statement means that you have access to all elements in the same package (for which you are writing your code) without any import statements. But this is true for all packages.
Yes. If you have a project set up in Eclipse, Netbeans, or another IDE, you can attempt to add a class to a project without adding a package. It'll usually say something like "Adding classes to the default package is not recommended," but allow you to add that class to the default package anyway. Alternatively, you could attempt to run
javac
on a.java
without a package, which would work assuming that all imports, syntax, etc. are correct.