Witscale Test Center

8.1 Java source file > 8.1.1 The top-level class and declaration rules


8.1.1 The top-level class and declaration rules

The java source file may contain several top-level class declarations. A top-level class is the one that is not a defined inside any other class. Among these multiple top-level class definitions, only one class can be public. In other words, a java source file can have at most one top-level public class.

If java source file contains a top-level public class, the name of that source file and the name of the top-level public class must be the same. For example, the public top-level class named HelloWorld must reside in a file named HelloWorld.java. If none of the top-level classes is public, the name of the source file can be any valid filename with the .java extension.