Nittin singla wrote:I am clear about the functionality of nested classes. But what i want to ask is when to use the nested classes.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:
Nittin singla wrote:I am clear about the functionality of nested classes. But what i want to ask is when to use the nested classes.
Truly nested classes (ie, a non-static inner class) are rare
Jeff Verdegan wrote:I'm pretty sure you've got the terminology backwards. A nested class is any class declared inside another class, and an inner class is a non-static nested class.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Amit J Thakur wrote:Is accessing the instance members of the enclosing class the only criteria for choosing between non-static vs static classes?
Amit J Thakur wrote:Is accessing the instance members of the enclosing class the only criteria for choosing between non-static vs static classes?
Mike Okri wrote:A nested class must be static if it has static members or if it accesses static members of its outer class; otherwise, it must be a non-static.
Amit J Thakur wrote:Can someone provide an example of nested non-static class and explain why static nested class would not be correct, and vice-versa.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
That's pretty much it in my book.
Amit J Thakur wrote:Jeff, could you give me the name of the book you are referring to.
Amit J Thakur wrote:Are there any well known libraries/classes which uses Nested (both static and non-static classes). I can take a look at it to understand the concepts better.
SCJP
Visit my download page
Stephan van Hulst wrote:Haha, don't worry about it. Jeff used a common English expression which basically means he strongly believes something.
Amit J Thakur wrote:The object of non-static nested class has an implicit reference to the enclosing class object, hence it does not get garbage collected even when the client class makes the list=null.
...Some developer can create a map, assign the reference of the Map.Entry objects to some variable and then assign null to the map object (As per my understand this map object will be marked for gc.). But as the existence of static nested objects doesnt require enclosing class object, these object(Map.Entry) will be there in the memory as they are still being referenced by some variable. If this is true, is this a good design as the existence of Map.Entry doesn't make sense without the enclosing Map object.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote: Basically, you're correct, except that it's probably easier to say that an inner class is not eligible for garbage collection until it's enclosing instance is.
To me it makes perfect sense that you wouldn't want to keep an entire Map around just because some idiot holds on to a single Entry like a limpet.
Amit J Thakur wrote:Am I missing something here? As per my understanding, an inner class will be eligible for garbage collection unless it referenced explicitly by either an enclosing class or some other outside class.
My point was that someone may inadvertently hold a reference to Entry objects and whether the possibility exists [in nested static classes]. The conclusion is that it is possible to do this but one needs to avoid such situation, advertently or inadvertently. But as a design, can we enforce, so that such mistakes can be avoided by the user of such classes.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
My apologies; I was completely wrong (Not the first time, won't be the last. I had to go back and re-read the chapter on inner (non-static) classes because I use them so rarely).
Just FYI, another concrete example of an inner class are the "views" returned by java.util.Map implementations. Have a look at keySet(), entrySet() and values() methods.
I've always found it easiest to think of a static nested class as just a regular class that happens to be defined inside another one for semantic reasons.