• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Nested vs Inner

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the difference between Nested class and Inner class? In which circumstances can an Inner class not be a nested class?
TIA...
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A top-level nested class is an "inner class" that is defined as static. As such, it is not truly an inner class at all. You can look at the JLS §8.1.2 Inner Classes and Enclosing Instancesn for more details.
Hope that helps,
Corey
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Definition: A nested class is any class whose declaration occurs within the body of another class or interface.
Definition: A top level class is a class that is not a nested class.
Definition: An inner class is a nested class that is not explicitly or implicitly declared static.
There was a time in the history of Java when people used the word �inner� class for both the static and the non-static kind. With the second edition of the JLS, inner classes are non-static.
This is how the JLS defines these terms. Some exam prep books have different meanings for these words.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The definition of inner class used to bother me. Why do they say �explicitly or implicitly� declared static? What does it mean to be implicitly declared static?
A class declared in another class either has the modifier static or it doesn�t. If there is no modifier, the class is an inner class.
A class declared in an interface is implicitly static. It is not an inner class.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, I got carried away. Let�s try again.
>>What is the difference between Nested class and Inner class?
An inner class is always a nested class. Some nested classes (static member classes) are not inner classes.
>>In which circumstances can an Inner class not be a nested class?
Never.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The organization is this:
A class is either (1) a top-level class or (2) a nested class.
If a class is a nested class, it is either (1) a member class, (2) a local class or (3) an anonymous class.
Member classes declared in classes are either static or inner.
Member classes declared in interfaces are static.
Local classes and anonymous classes are always inner.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Some people say an instance of an inner class always has a reference to an instance of an enclosing class. That is how they define inner class.
But that is not true.
Some inner classes are declared in a static context - in a static method, static initializer or an explicit constructor invocation parameter.
To be safe, use the JLS definition. An inner class is a nested class that is not explicitly or implicitly declared static.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
good knowledge in JLS Marlene.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Jose. I hope somewhere in that core dump Paulo can find the answer to his question.
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here are related discussions:
standard terminology on inner class
Inner classes and 1.4 exam
 
Paulo Freitas
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot for the time spent on this question Marlene!!!
More comprehensive explanation than that would be impossible!!!
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Paulo. It just tumbled out, one thought after another. I was worried that I overloaded you.
The definitions of inner class and static member class taken together can drive you in circles. There is no getting around it. Each definition refers to the other.
Beware that quite a few authors use inner class when they mean nested class. Gradually everyone is learning and using the JLS definitions. And beware that some exam prep books also use the old definitions (top level nested class, static inner class � crazy).
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Marlene,
Your explainations have helped me alot. However, I am still struggling with one of your posts. It is possible that I am 99% clear and need that last piece of puzzle. Or there is logical inconsistency in two statemets made by you:
Statement 1:


Some inner classes are declared in a static context - in a static method, static initializer or an explicit constructor invocation parameter.


Statement 2:


To be safe, use the JLS definition. An inner class is a nested class that is not explicitly or implicitly declared static.


Per my reading both statements can not be true. Statement 1 is saying that inner class can be implicitly static. Statement 2 is catagorically denying that.
Barkat
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marlene is right. This just means that the body of a local method of a class defined in a static context has no access to instance variables of the enclosing class. Of course, final local variables from the surrounding method are accessible.
 
My honeysuckle is blooming this year! Now to fertilize this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic