- Introduction to JVM Languages
- Vincent van der Leun
- 180字
- 2021-07-02 21:46:34
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 {
}
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.