Back to Top

Sunday, 29 January 2012

JAVA: Nested Class

 The class which is defined within another class is called nested class.
It can be divided into 2 types. They are -   static nested class & non-static nested class


Static Nested Class: 
  • Nested class which is declared static is static nested class.
  • Its methods & variables are associated with is mother/outer class.
  • It can be used only through object reference.
  • It is accessed using enclosing class name. Example: MotherClass.StaticNestedClass
  • It interacts with enclosing class or other class like any other top-level class.



Non-Static Nested Class/Inner Class:
  • It has access to the other members of the mother class, even they are private.
  • It can't define any static member itself.
  • Instance of inner class only valid within the instance of the enclosed class, & has direct access to the methods & fields of the enclosing instance.
  • To instantiate innerclass, 1st we need to instantiate the mother class. Example:
MotherClass.InnerClass iObj = MotherObject.new InnerClass();
 
Types of Inner Class
1.                 Local Inner Class 
2.                 Anonymous Inner Class 
 
Local Inner Class: 
·         The inner class which is declared within the body of a method is known as local inner class
·         It is not a member the enclosing class, hence doesn’t have any access specifies.
·         It has access to all the members of the enclosing class, even any final variable 
          within the scope.
 
Anonymous Inner Class:
·         The inner class which is declared within the body of a method and has no name 
          is known as anonymous inner class.
·         It is accessible only at the point of its declaration.

4 Responses to “ JAVA: Nested Class ”

Anonymous said...
29 January 2012 at 18:53

dis blog site is gettin betuh day by day....d best nested class expltion i've seen so far.thnx admin


anij said...
5 March 2012 at 02:02

Thanks a lot!! :)


Rahul said...
30 September 2015 at 15:45

Thank you, I had been looking for some online reference to be used in my Java training and your work really helped me.
Java J2EE Training in Chennai | Java J2EE Training in Chennai | Java J2EE Training in Chennai


Naviya Nair said...
24 October 2016 at 15:19

I have read your blog its very attractive and impressive. I like it your blog.

Java Training in Chennai Java Training in Chennai | Core Java Training in Chennai

Online Java Training Java 8 Online Training | Java J2EE Online Training | JavaEE Training Institute in Chennai Java Training Institutes

Java Training Institutes in Chennai Java Training in Chennai


Post a Comment

Popular Posts

Subscribe via Email
Subscribe Java Interview Questions via Email
All Rights Reserved JAVA INTERVIEW QUESTIONS | Privacy Policy | Anijit Sarkar
blog links Web Directory Academics Blogs