Inner Class

Java Inner classes are classes within class. An inner class instance has a special relationship with outer class. This special relationship gives inner class access to a member of the outer class as if they are the part of the outer class.

Note: Java Inner class instance has access to all member of the outer class(Public, Private & Protected)

Syntax for creating Inner Class

//outer class
class OuterClass {
//inner class
class InnerClass {
    }
}

Type of Inner class

  • Static
  • Method Local
  • Anonymous
  • Other then above these normal inner class

Normal Inner Class

Inner classes which are not method local , static or anonymous are normal inner class.

//outer class
class OuterClass {
//inner class
class InnerClass {
    }
}

If you compile the above code it will produce two class files.

outer.class
inner$outer.class
 Note: You can’t directly execute the inner class’s .class file with java command.

As it is not a static inner class so we can’t use static keyword with it.

How to access Inner Class

An inner class can be accessed only through a live instance of outer class.

Within Outer Class

The outer class can create an instance of the inner class in the same way as normal class members.

class OuterClass {
private int i = 9;

// Creating instance of inner class and calling inner class function
public void createInner() {
InnerClass i1 = new InnerClass();
i1.getValue();
}

// inner class declarataion
class InnerClass {
public void getValue() {
// accessing private variable from outer class
System.out.println("value of i -" + i);
}
}
}

From Outside Outer Class

Create an outer class instance and then inner class instance.

class MainClass {

public static void main(String[] args) {
// Creating outer class instance
OuterClass outerclass = new OuterClass();
// Creating inner class instance
OuterClass.InnerClass innerclass = outerclass.new InnerClass();
// Classing inner class method
innerclass.getValue();
}
}

Above code can also be replaced with

OuterClass.InnerClass innerClass = new OuterClass.new InnerClass();

this keyword

There are some rules associated with this and it refer the currently executing object. So in case of Inner class “this” keyword will refer the currently executing inner class Object. But to get this for outer class use “OuterClass.this”.

Modifiers Applied

Normal inner class will be treated as a member of the outer class so it can have several Modifiers as opposed to Class.

    • final
    • abstract
    • public
    • private
    • protected
    • strictfp

Note: Don’t get confused with the modifiers of Class and Inner Class. They are completely different.

Method Local Inner Class

When an inner class is defined inside the method of Outer Class it becomes Method local inner class.

Syntax for Creating Method Local Inner Class

class OuterClass {
private int i = 9;

// Creating instance of inner class and calling inner class function
public void innerMethod() {
// inner class declarataion inside method
class InnerClass {
public void getValue() {
// accessing private variable from outer class
System.out.println("value of i -" + i);
}
}
//inner class instance creation
InnerClass i1 = new InnerClass();
i1.getValue();
}
}

Now the definition of inner class is inside a method in Outer class. Still, the instance of the outer class can be created but only after the definition of inner class as you can see above.

Note:
  • Method local inner class can be instantiated within the method where it is defined and nowhere else.
  • Method local inner class can not use the variable defined in a method where it id defined still it can use the instance variable.
  • If method local variable is “Final” method local inner class can use it. (* Now variable is Final)

Modifiers Applied to Method Local Inner Class

Method local inner classes are eligible for modifiers like local variables so an method local inner class can have final or abstract.

Cheatsheet

  • Inner class is a member of the enclosing class.
  • Outer class reference is required to initiate inner class.
  • Inner class is of 4 types.
  • Inner classes defined within method are method local inner class.
  • Method local inner class can not access method local variables.
  • Final & Abstract are the only modifiers available to method local inner class.
  • Anonymous inner class don’t have any name.
  • Inner classes having Static modifier are known as Static inner class.
  • Static nested class can not access non static member of outer class.

 

References

22 Comments Inner Class

  1. janakiraman

    my college book readingin this program very nic eand this program is helpful of rank holder in 2015 in aadiparasakthi engineering college in melmaruvathur

    Reply
  2. Samhith

    **Method local inner class can not use the variable defined in method where it id defined still it can use the instance variable.**

    I guess this statement seems to be incorrect.

    public class OuterClass {
    private int j = 10;
    public void Method1() {
    int i = 100;
    class InnerClass {
    public void method_inner() {
    System.out.println(j);
    System.out.println(i);
    }
    }
    InnerClass ic = new InnerClass();
    ic.method_inner();
    }

    public static void main(String[] args) {
    OuterClass oc = new OuterClass();
    oc.Method1();
    }
    }

    // out put of the program is
    10
    100.
    which is contradiction to the top statement. Am I missing something?

    Reply
    1. V Gautam

      Hi Samith,

      I don’t know how you able to compile this code. THis code can not be compiled ans reason is same. Either you make variable i as final else you will not be able to access it.
      Please check it one more time.

      Regards
      Gautam

      Reply
      1. Mohit Shaha

        I think you might be using JDK 8 because JDK 8 allows to access non-final variables in inner class method

        Reply
  3. Amol tholbare

    yes u can write CAN WRITE TWO METHODS WITH SAME NAME AND SAME ACCESS SPECIFIER AND DIFFERENT RETURN TYPE……

    Reply
  4. GOPINATH

    CAN WE WRITE TWO METHODS WITH SAME NAME AND SAME ACCESS SPECIFIER AND DIFFERENT RETURN TYPE……………

    Reply
    1. Vivekanand Gautam

      Hi Lohith,

      us of this and super is completely difference. this is used to invoke from same object and super is used to invoke from super class. And only of them can be used at once.

      It doesn’t make any sense to use super(this). So you can’t use anything like this.

      Please let me know what you exactly want to achieve with this syntax??

      Thanks

      Reply
  5. anon

    OuterClass.InnerClass innerClass = new OuterClass.new InnerClass();

    Does this compile..?? It didn’t for me…

    but “OuterClass.InnerClass innerClass = new OuterClass().new InnerClass();” did..

    Reply
  6. Siddhartha De

    Thanks. As a student of Computer Science, this type of articles are very helpful. Please continue and enrich.

    Reply

Leave A Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.