What is a Compilation Unit in Java?
A compilation unit is the highest level syntactic structure that a java virtual machine can recognize and understand. A compilation unit consists of the following components:
- package directive
- import directive
- class declaration
- interface declaration
Only one of the classes or interfaces present in a compilation unit can be declared public. Compilation units are generally single source code file.
Apart from saving a compilation unit into files, java supports compilation units to be stored in a database. In case, compilation units are stored in a database, the limit of one public class or interface per compilation nit does not hold good. This is only possible as long as there is no way these compilation units are not extracted and placed in an individual file.
Every compilation unit is part of exactly one package. The package is specified by the package directive. If there is no package directive, the compilation unit is a part of the default package.