Class access modifiers

The visibility of classes can be adjusted. When not explicitly specified (as in the example earlier), the visibility is called package private. This means that the class can only be referenced to and instantiated by classes in the same package. See the previous chapter for an extensive explanation of how packages work on a JVM.

In most cases, you want to create classes that can be referenced and instantiated anywhere else. When adding the access modifier public before the class keyword, classes in any package can see this class and, usually, create instances from this class:

    public class ClassName {
}
Public classes can still have private constructors . When that's the case, the class can still be seen and referenced by classes in other packages, but they cannot be instantiated by code that cannot access the constructor.

An unusual requirement of the Java programming language is that a source code file can only define one public class and that the filename must match the class name exactly. Other JVM languages usually do not have this limitation.