CompilationUnit is the goal symbol (§2.1) for the syntactic grammar (§2.3) of Java programs. It is defined by the following productions:
CompilationUnit:
PackageDeclarationoptImportDeclarationsopt
TypeDeclarationsopt ImportDeclarations:
ImportDeclaration
ImportDeclarationsImportDeclaration TypeDeclarations:
TypeDeclaration
TypeDeclarationsTypeDeclaration
Types declared in different compilation units can depend on each other, circularly. A Java compiler must arrange to compile all such types at the same time.
A compilation unit consists of three parts, each of which is optional:
package
declaration (§7.4), giving the fully qualified name (§6.7) of the package to which the compilation unit belongs
import
declarations (§7.5) that allow types from other packages to be referred to using their simple names
Every compilation unit automatically and implicitly imports every public
type name declared in the predefined package java.lang
, so that the names of all those types are available as simple names, as described in §7.5.3.