Java – Access Modifiers

Access Modifiers:

  • Access modifiers are used to set permissions to access the Class and its members (variables, methods & constructors).
  • Java supports 4 access modifiers
    • private
    • <package> or <default>
    • protected
    • public

Note: We cannot apply access modifiers to blocks(we cannot access because no identity)

public class Pro
{
            private int x;
            private Pro()
            {          
            }
            protected void fun()
            {
            }
            static
            {
                        // Error:
            }
}

Understanding the Access Modifiers with simple diagram:

Representing the above diagram in table form:

Access ModifierWithin the ClassWithin the PackageSub class of Same packageSub class of other packageOther package
privateYesNoNoNoNo
<default>YesYesYesNoNo
protectedYesYesYesYesNo
publicYesYesYesYesYes

Scroll to Top