JVM Knowledge

Not all classes are specified by a class declaration. The following constructs implicitly declare classes:

  • a class instance creation expression that ends with a class body (§15.9.5)
  • an enum constant that ends with a class body (§8.9.1)
  • a compact compilation unit (§7.3)

In all cases, the members of any implicitly declared class, including any implicitly declared members, are subject to the usual rules for member declarations in a class.

The following production from §7.3 is shown here for convenience:

CompactCompilationUnit:
  {ImportDeclaration} {ClassMemberDeclarationNoMethod} MethodDeclaration {ClassMemberDeclaration}

ClassMemberDeclarationNoMethod:
  FieldDeclaration
  ClassDeclaration
  InterfaceDeclaration
  ;

The class implicitly declared by a compact compilation unit satisfies the following properties:

  • It is a top level class (§7.6).

  • Its name is a valid identifier (§3.8) determined by the host system.

    In simple implementations of the Java SE Platform, where compilation units are stored in files, the name of this implicitly declared class would typically be the name of the file containing the compact compilation unit minus any extension (such as .java or .jav).

  • It is not abstract (§8.1.1.1).

  • It is final (§8.1.1.2).

  • It is a member of an unnamed package (§7.4.2) and has package access.

  • Its direct superclass is Object (§8.1.4).

  • It does not have any direct superinterfaces (§8.1.5).

  • The body of the class contains every class member declared in the compact compilation unit (these are declarations of fields (§8.3), methods (§8.4), member classes (§8.5), and member interfaces (§9.1.1.3)). It is not possible for a compact compilation unit to contain declarations of instance initializers (§8.6), static initializers (§8.7), or constructors (§8.8).

  • It has an implicitly declared default constructor (§8.8.9).

It is a compile-time error if this class does not declare at least one candidate main method (§12.1.4).

Note that an unnamed package may have multiple implicitly declared classes as members.