Java Access Modifiers

Quang Nguyen
quangtn0018
Published in
1 min readSep 3, 2018

Here a list of access modifiers in Java and what they entails if you decide to use them.

Restrictive spectrum

Least ← — — — →Most

Private, Default, Protected, Public

Private:

  • only accessible inside a class

Default (no access modifier specified):

  • accessible inside class, same package class and subclass

Protected:

  • same as default with the addition of accessibility inside other package subclasses

Public:

  • accessible everywhere

The following are access modifiers that doesn’t really pertain to the restriction rules above and can be used in conjunction with the access modifiers above.

Static:

  • methods can only have access to other static methods and fields of the class

Final:

  • used to create constant variables
  • prevent method overriding
  • prevent inheritance

--

--