GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Difference between inner and nested classes ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Difference between inner and nested classes ?" Watch "Difference between inner and nested classes ?" New topic
Author

Difference between inner and nested classes ?

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Could somebody enlighten me on the difference between inner and nested classes ???
My understanding is that inner classes are those classes defined in methods while nested classes are those which are not part of any method but are defined within a class directly.
Is this understanding correct ?
If yes, are not inner classes also to be considered as nested classes, since they have an enclosing class anyway ?
What about anonymous classes defined in a method ? Is it also an inner class ?
What about an anonymous class defined at the class level like the one below ? Is it an inner class or a nested class ?
public class A {
Button myButton = new Button();
myButton.addActionListener (new ActionListener() {
public void actionPerformed(ActionEvent e ) { }
} );
}
Please help me clarify my doubt.
Appreciate your help . . .
Thanks,
Shashank
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Any class defined inside another class is a nested class. An inner class is a nested class that's not static, explicitly or explicitly.
Nested classes includes static member classes and inner classes.
Inner classes includes non-static member classes, local classes, and anonymous classes.
Inner classes often have enclosing instances, but may not if they are defined in static contexts.
Could somebody enlighten me on the difference between inner and nested classes ???
My understanding is that inner classes are those classes defined in methods while nested classes are those which are not part of any method but are defined within a class directly.
Is this understanding correct ?
No. Classes defined inside methods are local or anonymous classes, which are types of inner classes, but not the only ones.
If yes, are not inner classes also to be considered as nested classes, since they have an enclosing class anyway ?
All inner classes are indeed nested classes, but neither definition has anything to do with whether or not they have enclosing instances.
What about anonymous classes defined in a method ? Is it also an inner class ?
Yes.
What about an anonymous class defined at the class level like the one below ? Is it an inner class or a nested class ?
<code><pre>public class A {
Button myButton = new Button();
myButton.addActionListener (new ActionListener() {
public void actionPerformed(ActionEvent e ) { }
} );
}</pre></code>
This won't compile. At the class level, you can only have a declaration (of a field, method, constructor, or class) or a static initializer or instance initializer. You can't have a plain statement like myButton.addActionListener() - that needs to be inside a method, constructor, or initializer instead. If it were, then the class would be nested and inner.


"I'm not back." - Bill Harding, Twister
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Thanks Jim for the detailed explanation.
I understand it much better now.
--Shashank
 
GeeCON Prague 2014
 
subject: Difference between inner and nested classes ?