Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

SCJP Self test question: navel gazing  RSS feed

 
jay sugrue
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the Sierra-Bates self test questions on Inner Classes (Chapter 8) there is this following question on which I seek some verification.



The answer given to this question is 21 not 15. I understand that class Gazer, as an inner class
has access to class Navel's private and static members. So when the go and then adder method's
are invoked and 'size' is returned it is the Navel's static 'size' which is 7. But before that doesn't the
go method reassign 'size' to 5 ?
Does it do this anyway but the adder method can only see the static 'size' (7) and not the method local
'size' (5) ?

PS. Will be without internet access probably until tomorrow afternoon - so won't be able to reply until then.
Thanks.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the 'size' on line 9 is local to that method. So, there are two variables named size. However, when you get into the Gazer.adder() method, it can't see the local variable, only the class member, which is 7.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jay sugrue wrote: But before that doesn't the
go method reassign 'size' to 5 ?


It assigns the value 5 to a different size variable, one that is local to that method.

Does it do this anyway but the adder method can only see the static 'size' (7) and not the method local
'size' (5) ?


No, it doesn't reassign the member variable, but yes, it's true that adder() can only see the member and not the local. Your question there is a bit odd. The first half of it suggests that you're not aware that there are two distinct size variables, but the second half suggests that you do. Staring at it a bit too long perhaps? Time to step away for a bit?
 
jay sugrue
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Fred, Jeff. Yeah, Jeff, I think that would be an understatement at this point. Definitely better for a couple days break from it.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!