Access modifiers are keywords (except for the default modifier) that in Java regulate the level of access to a class and class members (in other words, their scope in different parts of the program).
In total, there are four access modifiers in Java: public, protected, private, and default. Only two of them can be used for non-nested classes: public and default. For fields and methods, all four.
- public is a publicly accessible class or class member that can be accessed from any part of the program;
- protected visibility within the package, as well as in all inheritors of the class in which the class member with the protected modifier was declared;
- private members of a class have visibility only within the class in which they were declared;
- default by default, that is, when the access modifier was not specified at all using the keyword. Access to such classes and class members is limited to the package in which they were declared (“package visible”).