What are the different access modifiers?

  • private – Accessible only within this class and its inner/nested classes.
  • package-private – Accessible from all classes within the same package. No access for subclasses. Applied when no modifier is specified
  • protected – Accessible from all classes in the same package and all subclasses.
  • public – No access restrictions.

Access modifiers are applied to classes, interfaces, methods, and fields. They are essential for implementing the principle of least privilege (PoLP) and for separating the internal implementation from parts of the public API.

This topic is discussed in Effective Java Item 13 and in Section 6.6 of the specification.