• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Q. Inner classes

 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
SCJP Mock Exam # 1/
Java Certification Mock Exam # 1
http://www.certification4career.com/index.html
Question
29. An inner class created inside a method can access
A. Any local variables of a method that contain an inner class.
B. Any instance variables of the enclosing class
C. Any final variables of the enclosing class or a method that contain an inner class.
D. None of the above
Answer
Their answer : C
I thought
- any variable in enclosing class, and
- final local variables and arguments of the enclosing method.
 
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Karin Paola
29. An inner class created inside a method can access
A. Any local variables of a method that contain an inner class.
B. Any instance variables of the enclosing class
C. Any final variables of the enclosing class or a method that contain an inner class.
D. None of the above


A is wrong, because inner class can only access non-private local variables.

 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Karin,
Your thoughts are correct. A local inner class can access the variables of the enclosing class and the final local variables and final method parameters.
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh boy... semantics alert :roll:
An inner class in the narrow, JLS sense refers to a non-static nested class. Such classes always carry an implicit reference to the enclosing "outer" class.
Unfortunately, some documents use the term "inner class" in the broad sense to indicate any type of nested class, static or not. Even more unfortunately, those documents include some coming from Sun themselves
Personally, I think the first of these definitions makes a lot more sense than the second. YMMV. Beware, in any case. I will assume the test meant "inner class" in the narrow sense since anything else doesn't make sense.
Karin, you are right. An inner class can access instance variables in the enclosing class, private or not (and, oddly, those of other inner classes enclosed in the same class), and if the class is defined inside a method, also any final method-local variables (including but not restricted to method parameters). Of the four answers they give, B is best; C is wrong because it is too restrictive.
Don, I assume that's a typo and you meant to say "non-final" instead of "non-private".
- Peter
 
John Lee
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Peter den Haan:
Don, I assume that's a typo and you meant to say "non-final" instead of "non-private".


Thanks.
 
Karin Paola Illuminate
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys.
reply
    Bookmark Topic Watch Topic
  • New Topic