Type Definitions
Home ] Up ] Qualified Names ] The import Statement ] Automatic Import ] The package Statement ] Compilation Units ] Host Support for Packages ] [ Type Definitions ] Access Control ]

 

 

Within a compilation unit you may have several TypeDeclarations. However, note the following:

From "The Java Language Specification", by James Gosling, Bill Joy and Guy Steele, Addison-Wesley:

"When Java packages are stored in the file system, the host system may choose to enforce the restriction that it is a compile-time error if a type is not found in a file under a name composed of the type name plus an extension (such as .java or .jav) if either of the following is true:

  • The type is referred to by code in other compilation units of the package in which the type is declared, or:
  • The type is declared public (and therefore is potentially accessible from code in other packages).

This restriction implies that there must be at most one such type per compilation unit. This restriction makes it easy for a Java compiler and Java Virtual Machine to find a named class within a package. For example, the code for a public type wet.sprocket.Toad would be found in a file Toad.java in the directory wet/sprocket. The corresponding object code would be found in the file Toad.class in the same directory.

When Java packages are stored in a database, the host system need not enforce such restrictions.

In practice, many Java programmers choose to put each class or interface type in its own compilation unit, whether or not it is public, or is referred to by code in other compilation units."

This page was last modified on 02 October, 2007