File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes inner classes see everything? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "inner classes see everything?" Watch "inner classes see everything?" New topic

inner classes see everything?

Eduardo Pereira

Joined: Dec 12, 2001
Posts: 2
question seen in this site:
(#232) TRUE or FALSE: a
non-static inner class always
has free access to ALL member
data of its enclosing (outer)
Answer: TRUE
An inner class has a special
relationship and can see even private
member data of the outer class.
What if it is an anonymous inner class defined inside a method?? As far as I know those classes can only refer to final member variables of the outer class (wether public or private).
Am I right, or did I misunderstand the question?
Valentin Crettaz
Gold Digger

Joined: Aug 26, 2001
Posts: 7610
Moving this to the Study forum.

[Blog] [Blogroll] [My Reviews] My Linked In
Rob Ross

Joined: Jan 07, 2002
Posts: 2205
Eduardo, you're confusing "member" variables and "local" variables.
Member variables belong to the enclosing class, and are always available freely to the inner class.
Local variables usually cease to exist when the method in which they are defined exists; however, by marking these local variables final (including paramater variables, which are also local) you can use them in your inner class. An inner class may not use any non-final local variables.

SCJP 1.4
I agree. Here's the link:
subject: inner classes see everything?
It's not a secret anymore!