Hi, This question is from round up game of 'javaranch site' : A non-static inner class always has free access to all member data of its inclosing (outer) class. a. True b. False Answer given is 'True' But i don't think so because it does not always have free access to all member data of its inclosing class. Consider the case when inner class is defined inside a static method. If i am wrong, java expert please feel free to correct me. regards vivek
it cannot be static inner class as mentioned in the question, and inner class is enclosed from a static class , it will become static inner class.then it won't remain as non-static inner class. - i think this will resolve your problem.... thanks,
Joined: Nov 22, 2008
But i don't think so because it does not always have free access to all member data of its inclosing class. Consider the case when inner class is defined inside a static method well, where in the qstn does it say this? I would prefer to read the qstn to the extent it means and would not want to assume such things. While what you say is correct in the context mentioned by you, in the context of the given qstn the ans given is appropriate, IMO. Regds. - satya
Hi vivek, The question says "non-static inner class". If it was a class defined in a method, it would have said Local inner class.So, you should not assume or think about the class defined in a method. The given answer is absolutely correct. Suma
Vivek, I second your point and indeed IMHO you make quite a bit of sense.While we all know that the question probably excludes a local inner class in its definition of a "non -static inner class", without making any assumptions I dont see why and how one can say that a local inner class is not in the set of "non-static inner class".Needless to say that a local inner class is implicitly non-static.Further I disagree with a point above which states that a local inner class may become static by virtue of being defined inside a static method.This is not true in that sense, such a situation only has the effect of restricting access of the class to only those variables accessible to the method, which are generally static variables. Vivek, certain questions in mock tests demand you to make certain assumptions, but rest assured that if you understand the concepts, Sun attempts to a very high degree indeed to do away with ambiguity in its questions, and in an exam situation, believe me, you will be able to easily tell which of the four inner class types they are asking about, that is,member non-static,member -static, anonymous or method local.
Good luck, Herbert
Joined: Jun 03, 2000
Hi, Thanks to all of u. I think this is the result of what i did, i tried to understand question in a differnet way. yes herbert my conclusion of "non-static inner class" includes all the inner classes no matter whether top level or local inner classes. Suma/herbert thanks for making me clear, i won't do this mistake again. regards vivek
The term "inner class" is a general one, and it definitely does include member classes and local classes (and anonymous classes, which are a form of local class). It may or may not include static member classes (depending on what definition you use), but that's not important in this problem. What is important is that this problem asks about non-static inner classes. This term obviously excludes static member classes - the question is, does it exclude local classes in static contexts? I think it does, but that's not quite as obvious. Being in a static context is just like being static, except that it's unnecessary (and illegal) to actually declare that a local class is static. That information is already part of the context where the class is declared. So "non-static" seems like a reasonable shorthand for "not declared static, and not declared in a static context". I think that the question is probably not intended to test whether you know about static contexts. (I'm 99% sure they won't be on the exam.) I doubt Paul wants to put in extra words that will only confuse most of the people who read the question. However since the question does say "always" I suppose it would be best to make sure this loophole is completely closed. I can't think of a concise way to do this - perhaps it's better to ask the question a little differently: <blockquote>An inner class always has free access to all member data of its inclosing (outer) class, provided the inner class is not static or defined in a static context.</blockquote> Or take out the "always" and the "non-static" and focus on private data: <blockquote>An inner class can access private member data of its inclosing (outer) class.</blockquote> This isn't always true, but the it certainly is true in general. Note - if the question had meant to exclude all local classes as Suma suggests, it would (should?) have said member classes, not inner classes.