English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

How to access the static variable of an outer class from a static inner class in Java?

A class within another class is called an inner class unless it is a nested class, in which case it cannot be declared as a static class. A static nested class is like other class variables. You can access it (static nested class) without instantiation.

Example

You can access the static variables of an outer class just by using the class name. The following Java example demonstrates how to access class static variables from a static nested 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 Result

This is my nested class
200
You May Also Like