Access Modifiers in Java

Access Modifiers


The video below describes how the key access modifiers (public, protected, default, and private) are used with the classes, methods and variables. The video contains one error. It says that not all the keywords can be used with classes. But that is not correct. Classes in java can be declared private or protected.



Private Classes in Java


Here is an example:

class Outer_Demo {
   int num;
   
   // inner class
   private class Inner_Demo {
      public void print() {
         System.out.println("This is an inner class");
      }
   }
   
   // Accessing he inner class from the method within
   void display_Inner() {
      Inner_Demo inner = new Inner_Demo();
      inner.print();
   }
}
   
public class My_class {

   public static void main(String args[]) {
      // Instantiating the outer class 
      Outer_Demo outer = new Outer_Demo();
      
      // Accessing the display_Inner() method.
      outer.display_Inner();
   }
}