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


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

Updated on: 05-Aug-2019

819 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements