Java Inner Class

Categories: Java // Tags: , , .

// By:Team // No Comment

Email this to someonePrint this pageShare on Google+3Share on Facebook7Tweet about this on TwitterShare on LinkedIn0Share on Reddit0Pin on Pinterest0Share on StumbleUpon1


Java 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

 

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.

Email this to someonePrint this pageShare on Google+3Share on Facebook7Tweet about this on TwitterShare on LinkedIn0Share on Reddit0Pin on Pinterest0Share on StumbleUpon1

About Team

Browse Archived Articles by Team

Related

Sorry. There are no related articles at this time.