Sitemap

JAVA — Access modifiers

Jul 21, 2025

--

Press enter or click to view image in full size

In Java, access modifiers are keywords used to control the visibility (access level) of classes, variables, methods, and constructors.

Press enter or click to view image in full size

1. public

  • Accessible from anywhere (any class, package, or project)
  • Used when you → make a class/member to be accessible from any other class.

2. private

  • Accessible only within the same class
  • Used to hide internal details (data hiding = encapsulation)

Get hansini gunasena’s stories in your inbox

Join Medium for free to get updates from this writer.

3. protected

Accessible in:

  • The same package, same class
  • Subclasses ( in other packages)
  • useful for inheritance when you want to allow access only to child class.

4. (Default)— (Package private) no modifier

  • Accessible only within the same package
Press enter or click to view image in full size
Press enter or click to view image in full size

--

--