| Author |
Inner class doubt ?
|
Sim Kim
Ranch Hand
Joined: Aug 06, 2004
Posts: 268
|
|
Suppose there is a question : Inner class has access to all variables of enclosing class ? The answer should be True or False ? Reason I am asking this is because if we consider inner class as both static and non-static inner class then this is False . But if we consider inner class as non-static inner class ( or method-local inner class defined inside non-static method) then this is true .
|
 |
Mike Gershman
Ranch Hand
Joined: Mar 13, 2004
Posts: 1272
|
|
I hope an exam question will be more specific. Four kinds of variable: static variable instance variable non-final local variable final local variable Four kinds of nested class: static nested class inner class method-local inner class anonymous inner class This means 16 rules to learn. Most of them are obvious, as with your example of static nested classes not having access to instance variables. Some are subtle, like method-local classes not having access to non-final local variables. But they can all be on the test.
|
Mike Gershman
SCJP 1.4, SCWCD in process
|
 |
ankur rathi
Ranch Hand
Joined: Oct 11, 2004
Posts: 3829
|
|
I know that this is a height of expecting ... but can you mention 16 rules ... I will be thankful to you forever for this . [ January 10, 2005: Message edited by: rathi ji ]
|
 |
Mike Gershman
Ranch Hand
Joined: Mar 13, 2004
Posts: 1272
|
|
Suppose there is a question : Inner class has access to all variables of enclosing class ?
I was thinking more about this question. Technically, static nested classes are not inner classes. On that basis, the answer is generally true. The big exception is a static method-local class, which cannot directly access instance variables. However, that is equally true of static nested classes, static methods, and static initializers, so I would still answer your question "true".
|
 |
Mike Gershman
Ranch Hand
Joined: Mar 13, 2004
Posts: 1272
|
|
can you mention 16 rules ...
Rathi, what book are you using? I would rather point you to the correct section of your book than rewrite it here. If you don't have a book, the author has generously made "Thinking in Java" available online for free here http://www.mindview.net/Books/TIJ/
|
 |
ankur rathi
Ranch Hand
Joined: Oct 11, 2004
Posts: 3829
|
|
|
Mike I am using K&B book .But I havn't read that chapter of inner class . But I am sure that in any book I will not get such an important information regarding cirtification in such a concise form .
|
 |
Barry Gaunt
Ranch Hand
Joined: Aug 03, 2002
Posts: 7729
|
|
I've deleted a couple of sort of half off-topic posts here which were giving rise to a little friction heat... [ January 11, 2005: Message edited by: Barry Gaunt ]
|
Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
|
 |
 |
|
|
subject: Inner class doubt ?
|
|
|