What is a Compilation Unit in Java?

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.

Share and Enjoy !

See also  Class FileDescriptor in Java

One thought on “What is a Compilation Unit in Java?

  1. Thanks , I have recently been searching for info about this topic for ages and yours is the best I’ve found out till now. But, what concerning the bottom line? Are you sure about the source?

Leave a Reply

Your email address will not be published. Required fields are marked *