How to access Static variable of Outer class from Static Inner class in java?

Java 8Object Oriented ProgrammingProgramming

A class with in another class is known as inner class, you cannot declare a class static unless it is an inner class. A static inner class is just like other class variables. You can access it (static inner class) without instantiation

Example

You can access the static variable of an outer class just using the class name. Following Java example demonstrates how to access static variables of a class from a static inner class.

public class Outer {
   static int data = 200;
   static class InnerDemo {
      public void my_method() {
         System.out.println("This is my nested class");
         System.out.println(Outer.data);
      }
   }
   public static void main(String args[]) {
      Outer.InnerDemo nested = new Outer.InnerDemo();
      nested.my_method();
   }
}

Output

This is my nested class
200
raja
Published on 05-Aug-2019 18:09:31
Advertisements