• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Difference between inner and nested classes ?

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.


 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jim for the detailed explanation.
I understand it much better now.
--Shashank
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic