Java: Inner Classes

×

Static Inner Classes

Overview

  • The javac Compiler will create 2 classes : InnerClassDemo1.class, InnerClassTest$1.class
  • nner Classes can have static and/or object variables/methodes
  • Static data member/functions can be accessed before ANY class instantiation
  • his is true for nested/inner classe too
  • Syntax calling static functions : NestedClass.myNestedStaticMethod()
  • Syntax accessing static data members : NestedClass.CPU, InnerClassDemo1.pcName
  • Serialization of inner classes, including local and anonymous classes, is strongly discouraged
  • A static nested class does not have access to the instance variables and methods of the outer class

Java Code

public class InnerClassDemo1 {
   static String pcName = "Laptop"; 	
   
   static void accessNestedDataMember() {
   		System.out.println("My " + pcName + " is equiped with an " + NestedClass.CPU + " CPU");
	}
	
   static class NestedClass {
   	  private String memory = "16 Gbyte";
      static  String CPU = "Intel i7";
      public void myNestedMethod() {
         System.out.println("My " + pcName + " is equiped with  " + memory + " memory");
      }
      static void myNestedStaticMethod() {
         System.out.println("My " + InnerClassDemo1.pcName + " is equiped with  " + CPU + " CPU");
      }
   }
   
   public static void main(String args[]) {
   	  System.out.println("CPU Type: " + NestedClass.CPU);
   	  InnerClassDemo1.accessNestedDataMember();
   	  NestedClass.myNestedStaticMethod();
   	  /*
   	  InnerClassDemo1.myNestedMethod(); 
   	    --> fails with Error because myNestedMethod() is not static !
		D:\JAVA\tutorial\Innerclass>javac InnerClassDemo1.java
		InnerClassDemo1.java:19: error: cannot find symbol
          InnerClassDemo1.myNestedMethod();
        --> Here we need to instantiate our InnerClassDemo1 Object first
   	  */
      InnerClassDemo1.NestedClass ict = new InnerClassDemo1.NestedClass();	 
      ict.myNestedMethod();
   }
}

Output

D:\JAVA\tutorial\Innerclass>java InnerClassDemo1
CPU Type: Intel i7
My Laptop is equiped with an Intel i7 CPU
My Laptop is equiped with  Intel i7 CPU
My Laptop is equiped with  16 Gbyte memory

Anonymous Inner Class

  • Syntax: The syntax of an anonymous class expression is like the invocation of a constructor, except that there is a class definition contained in a block of code.

Sytax Details

// Test can be interface,abstract/concrete class
Test t = new Test() 
 
{
   // Anonymous Inner Class data members and methods
   public void test_method() 
   {
      ........
      ........
    }

};
    

Java Code without using Anonymous Inner Class

interface Age
{
    int x = 21;
    void getAge();
}
public class AnonymousDemo1
{
    public static void main(String[] args) 
    {
        // Myclass is implementation class of Age interface
        MyClass obj=new MyClass();
 
        // calling getage() method implemented at Myclass
        obj.getAge();     
    }
}
 
// Myclass implement the methods of Age Interface
class MyClass implements Age
{
    @Override
    public void getAge() 
    {
        // printing the age
        System.out.print("Age is "+x);
    }
}

Java Code using Anonymous Inner Class

interface Age
{
    int x = 21;
    void getAge();
}

public class AnonymousDemo2
{
    public static void main(String[] args) {
 
        // Myclass is hidden inner class of Age interface
        // whose name is not written but an object to it 
        // is created.
        Age oj1 = new Age() {
            @Override
            public void getAge() {
                 // printing  age
                System.out.print("Age is "+x);
            }
        };
        oj1.getAge();
    }
}

Reference Anonymous Inner Class Tutorial

Inner Class Sample

Reference Inner Class Sample