This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Anonymous class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Anonymous class" Watch "Anonymous class" New topic
Author

Anonymous class

Milan Sutaria
Ranch Hand

Joined: Jul 10, 2008
Posts: 118

Inside main method an anonymous class is created which is a sublclass of Thread class. How can it access the go method without the class name ( B.go() )?


SCJP 6 83%
Working on SCWCD/OCPJWCD
Bob Ruth
Ranch Hand

Joined: Jun 04, 2007
Posts: 320
Inner classes have access to all members of the enclosing outer class.


------------------------
Bob
SCJP - 86% - June 11, 2009
Milan Sutaria
Ranch Hand

Joined: Jul 10, 2008
Posts: 118
i was thinking something else .... thanks for helping out.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Although you have got your answer but still I would like to explain it here
As you might know All Inner classes are compiled into top level classes.
When an anonymous class accesses the members of its enclosing class at compile time the compiler will add the name of the enclosing class at the statement where you are accessing the enclosing class member(static).
So your code after compilation will become-

[ August 05, 2008: Message edited by: AnkitJi Garg ]

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Anonymous class
 
Similar Threads
Anonymous Issue
how to start a thread??
StackOverflowError error at Runtime :(
Anonymous Class
How can anonymous class extend any other class?