HomeCore JavaJava inner class with examples

Java inner class with examples

1. What is java inner class?

The Java programming language allows you to define a named no static class within another class. Such a class is called inner class or non static inner class. A java inner class is a named non static class with in a class.

1.1. Example

//outer class
class Outer {
     //inner class
     class Inner {
     }
}

when inner classes compiled, for every inner class a separate java class file will be generated asfollowing. 

  1. Generated class file for Outer.java:   Outer.class 
  2. Generated class file for Inner.java:   Outer$Innner.class

2. main method in inner classes

Inside inner class we can’t declare static members. So that it is not possible to declare main() method inside non static inner class. But static inner classes allowed to write static members, that will discuss in Static nested inner classes area. for the below program you will get compile time error.

public class Outer {
 
    class Inner {
        
        // Static members are not allowed inside inner class
        public static void main(String[] args) {
            System.out.println("Non static inner class");
        }
        
        // Static members are not allowed inside inner class
        public static void add(int a, in b) {
            System.out.println(a+b);
        }
      }
     }

Output :

You will get compiler errors at line 6 and 11 as following:

“The method xxxxx cannot be declared static; static methods can only be declared in a static or top level type”

3. Accessing inner class code from Outer class static area

public class Outer {
 
    class Inner {
        
        public void add(int a, int b) {
            System.out.println(a+b);
        }
    }
    
    //Accessing iiner calss method inside Outer class from main method
    public static void main(String[] args) {
                 //Inner inner = new Inner(); not possible this in static area
         // First way
         Outer outer = new Outer();
         Inner inner = outer.new Inner();
         inner.add(20, 30);
         
         // Second way (just simplification of first way)
         new Outer().new Inner().add(30, 40);
    }
  }

Output :

50
70

4. Accessing inner class code from instance area of Outer class

public class Outer {
 
    class Inner {
        
        public void add(int a, int b) {
            System.out.println(a+b);
        }
    }
    
    // Outer class instance method
    public void deposit(int a, int b) {
        Inner inner = new Inner();
        inner.add(a, b);
    }
    
 
    public static void main(String[] args) {
         // First way
         Outer outer = new Outer();
         outer.deposit(10, 20);
         outer.deposit(40, 60);
    }
}

Output :

30
100

5. Accessing inner class code from out side of Outer class

public class Outer {
 
    class Inner {
        
        public void add(int a, int b) {
            System.out.println(a+b);
        }
    }
}
public class TestInner {
 
    public static void main(String[] args) {
        
        // Calling inner class add method fromm out side class
        new Outer().new Inner().add(20, 30);
        
        new TestInner().deposite(30, 40);
    }
    
    public void deposite(int a, int b) {
        new Outer().new Inner().add(a, b);
    }
}

Output :

50
70

6. Accessing Outer class members from inside of inner class

From inner class we can access all members of outer class (both static and non-static, private and non private methods and variables) directly.

class Outer {
    
    int x = 10;
    static int y = 20;
    
    class Inner {
        public void add(){
            System.out.println(x);//10
            System.out.println(y);//20
            System.out.println(x+y);//30
        }
    }
    
    public static void main(String[] args){
        new Outer().new Inner().add();
    }
}

7. Shadowing and use of this keyword inside inner class

Within the inner class this always refers current inner class object. To refer current outer class object we have to use “outer class name.this”.

class Outer {
    
    int x=10;
    
    class Inner {
        
        int x=100;
        
        public void methodOne() {
            int x=1000;
            System.out.println(x);//1000
            System.out.println(this.x);//100
            System.out.println(Outer.this.x);//10
        }
    }
    
    public static void main(String[] args){
        new Outer().new Inner().methodOne();
    }
}

Output :

1000
100
10

8. Allowed modifiers for inner classes

The applicable modifiers for outer classes are public, default, final, abstract, strictfp.

But for Inner classes in addition to the above modifiers private, protected, static also valid.

9. Conclusion

In this article we have covered all about java inner class, it is a named non static class with in class. There are other kind of non static inner classes that java allows

  1. Java local class
  2. Java Anonymous class.

References

  1. Java documentation

LEAVE A REPLY

Please enter your comment!
Please enter your name here