Inner Class

This entry is part 24 of 34 in the series Core Java Course

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


 

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.

If you compile above code it will produce two class file.

 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.


 

From Outside Outer Class

Create outer class instance and then inner class instance.

Above code can also be replaced with

 

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

 

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

21 Comments Inner Class

  1. Siddhartha De

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

    Reply
  2. 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
    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
  3. GOPINATH

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

    Reply
  4. Amol tholbare

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

    Reply
  5. Shivpujan Kumar

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

    Reply
  6. 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
  7. 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

Leave A Comment

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