Inner Classes in Java

Sometimes we can declare a Class inside another Class such types of Classes are Called inner Classes. In Java 1.1v inner classes concept was Introduced to fix bugs (GUI bugs) as a part of event handling but because of powerful features and benefits of Inner Classes slowly programmers started using irregular coding also.

We should go for inner classes if there is no chance for the existence of one object without the existence of another type of object.

Example-

There is no chance for existence of engine object without the existance of a vehicle object. We have to declare the Engine class inside the Vehicle class, In this case, Engine class is the inner class and the Vehicle is the outer class.

class Vehicle {  //Outer Class
    class Engine{ 
          //Inner Class
    }
}

Note-

  • If there is no outer class then there is no chance of inner Class.
  • The Relation between outer Class and Inner Class is a has a relationship.

Types of Inner Classes

According to position of declration and behaviour inner Class in Java are divided into four catogries.

  1. Normal or Regular inner classes
  2. Method Local inner classes
  3. Anonymous inner classes
  4. Static nested classes

Normal or Regular Inner Classes

If we declare any named class inside a class without static modifier, such type of error classes is known Normal or Regular inner classes.

class OuterClass {
        public static void main(String[] args) {
            System.out.println("inner class main method");
        }
        class InnerClass{
    }
}
 
Normal or Regular Inner Classes

Inside the Inner Class, we can not declare any static member, Hence we can not declare the main method and run the inner Class directly.

Accessing Inner Class code from static or Outer Class.

class OuterClass {
        class InnerClass{
            public void method1(){
                System.out.println("This is Inner class method");
            }
    }

    public static void main(String[] args) {
        OuterClass outer = new OuterClass();
        OuterClass.InnerClass inner = outer.new InnerClass();
        inner.method1();
        // outer
        OuterClass.InnerClass i = new OuterClass().new InnerClass();
        i.method1();
}}
Accessing Inner Class code from static or Outer Class.

Accessing Inner Class Code from Instance Area of Outer Class

class OuterClass {
        class InnerClass{
            public void method1(){
                System.out.println("This is Inner class method");
            }
    }
    public void method(){
            InnerClass inner = new InnerClass();
            inner.method1();
}

    public static void main(String[] args) {
        OuterClass object = new OuterClass();
        object.method();
    }
}
Accessing Inner Class Code from Instance Area of Outer Class

Accessing Inner Class Code from Outside of Outer Class


class OuterClass {
        class InnerClass{
            public void method1(){
                System.out.println("This is Inner class method");
            }
    }

    }
class TheCodeData{
    public static void main(String[] args) {
        OuterClass obj = new OuterClass();
        OuterClass.InnerClass i = obj.new InnerClass();
        i.method1();

    }
}
Accessing Inner Class Code from Outside of Outer Class

Accessing static and non static Member of Outer Claass inside Inner Class

We can access both static and nonstatic members of the outer class from the regular inner class directly.

class OuterClass {
    int num = 10;
    static int num2 = 100;
        class InnerClass{
            public void method1(){
                System.out.println("This is Inner class method");
                System.out.println(num);
                System.out.println(num2);
            }}

            public static void main(String[] args) {
                new OuterClass(). new InnerClass().method1();
            }
    }
Accessing static and non static Member of Outer Claass inside Innner Class

  • Inside the inner class, this always refers to the current inner class object but if we want to refer current outer class object then we have to use outerclass.this
package inncer;

class OuterClass {
    int num = 10;
        class InnerClass{
            int num = 20;
            public void method1(){
                System.out.println(this.num);
                System.out.println(OuterClass.this.num);

            }
        }
            public static void main(String[] args) {
                new OuterClass(). new InnerClass().method1();
            }
    }
outerclass.this

Modifiers for Outer Class and Inner Class

The only applicable modifier for outer class are –

  • public
  • default
  • final
  • abstract
  • strictfp

Applicable Modifier for Inner Class

  • public
  • default
  • final
  • abstract
  • strictfp
  • private
  • protected
  • static

Nesting of Inner Classes

We can declare Inner Class inside another Inner Class that is nesting of Inner Class is possible.

package inncer;

class OuterClass {
   class InnerClass1{
       class InnerClass2{
           public void method(){
               System.out.println("InnerClass2 static method");
           }
       }
   }
    }
    class TheCodeData{
        public static void main(String[] args) {
            OuterClass obj = new OuterClass();
            OuterClass.InnerClass1 innerClass1 = obj.new InnerClass1();
            OuterClass.InnerClass1.InnerClass2 innerClass2 = innerClass1.new InnerClass2();
            innerClass2.method();
        }
    }
Nesting of Inner Classes

Similar Java Tutorials

1 thought on “Inner Classes in Java”

Leave a Comment