Inner Class

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

Note: 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 above code it will produce two class file.

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

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


 

How to access Inner Class

Inner class can be accessed only through live instance of outer class.

Within Outer Class

Outer class can create instance of the inner class in the same way as normal class member.

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 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 like 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 definition of inner class is inside a method in Outer class. Still the instance of the outer class can be created but only after 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 no where else.
  • Method local inner class can not use the variable defined in 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 variable so an method local inner class can have final or abstract.

Cheatsheet

  • Inner class is member of enclosing class.
  • Outer class reference is required to initiate inner class.
  • Inner class are of 4 type.
  • Inner classes defined within method are method local inner class.
  • Method local inner class can not access method local variable.
  • Final & Abstract are the only modifiers available to method local inner class.
  • Ananymous 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.
Series Navigation<< Java Hashmap tutorialInheritance >>
By | 2012-04-21T05:33:04+00:00 April 21st, 2012|Core Java|21 Comments

About the Author:

21 Comments

  1. Siddhartha De October 28, 2013 at 4:01 am - Reply

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

  2. anon December 25, 2013 at 5:12 pm - Reply

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

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

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

    • Vivekanand Gautam December 25, 2013 at 5:26 pm - Reply

      Agree. Typo error. same has been rectified.

      Thanks

      • anon May 26, 2016 at 6:47 pm - Reply

        The byte code file should be written as Outer$Inner.class file

  3. vignesh January 7, 2014 at 7:22 am - Reply

    it’s really nice..i can able to understand thank you…

  4. venkat March 15, 2014 at 7:19 am - Reply

    it is really good material to learn java concepts

    • Rahul October 6, 2016 at 1:59 pm - Reply

      yaaa bro i also agree with you………………..

  5. SATENDRA May 19, 2014 at 3:26 am - Reply

    IT IS REALLY VERY HELPFUL THANKS FROM BOTTOM OF MY HEART

  6. lohith June 17, 2014 at 3:56 pm - Reply

    can we use super(this) …

    • Vivekanand Gautam June 17, 2014 at 5:09 pm - Reply

      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

  7. GOPINATH June 21, 2014 at 6:50 pm - Reply

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

    • Anmol Singh July 21, 2014 at 7:41 am - Reply

      no because in java return type does not comes under method signature

  8. Amol tholbare July 4, 2014 at 1:19 pm - Reply

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

  9. jasmine September 1, 2014 at 3:04 pm - Reply

    thanks that is very useful for me

  10. Shivpujan Kumar September 13, 2014 at 8:03 am - Reply

    A very nice tutorial i appreciate this work……….and also this tutorial is very helpful for me…
    Thanks

  11. sa April 28, 2015 at 10:31 am - Reply

    Good

  12. Samhith May 30, 2015 at 2:50 pm - Reply

    **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?

    • V Gautam May 31, 2015 at 12:22 pm - Reply

      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

      • Mohit Shaha May 13, 2016 at 9:25 am - Reply

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

  13. janakiraman October 12, 2016 at 8:04 am - Reply

    very nice thought and very useful comands

  14. janakiraman October 12, 2016 at 8:07 am - Reply

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

Leave A Comment