Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inner class doubt ?

 
Sim Kim
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic