Access modifiers in java

Access modifiers and Scope

Types of access modifiers

  1. Public - can be accessed in any Class in any package
  2. default - also known as package public. can be accessed in any class of same package.
  3. private can be only accessed in the same class
  4. protected - can be only accessed in the same package AS WELL AS SUBCLASSES in DIFFERENT PACKAGES.
Class Outer{
private variable A;
public printB(){
Inner I = new Inner();
System.out.println(“The value of inner class variable B accessible here is”+I.B);
}


Class Inner{
private variable B;

 //Inner constructor

Inner(){
 System.out.println(“The value of outer class variable A accessible here is”+A);
}

 }
}