Meaningless Drivel is fun!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Anon Inner Class ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Anon Inner Class ??" Watch "Anon Inner Class ??" New topic

Anon Inner Class ??

Rick Roberts
Ranch Hand

Joined: Apr 07, 2005
Posts: 59

Just when I thought I had it all figured out.
I come across a question like this:

package rroberts;
public class AnonInner3 {

public static void main( String[] args ) {
class A {
String name = "bob";
A a = new A() {
String name = "notbob";
System.out.println( );

The result is:

A. bob
B. notbob
C. compile error
D. runtime error

I know the answer is bob; but I dont understand why.
It seems that it would be notbob.
Anyone care to enlighten me?


"Stay on the path."
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2398
Since instance variables are hidden in subclasses, and the reference type of a is A, the instance variable that is referred to by the name will be the one in class A.
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
Run time binding or compile time binding?

Instance variables, static variables, static overridden methods (it looks like it's an override, but actually hidden), and overloaded methods are all bound at compile time; and this kind of binding depends on the type of the reference variable and not on the object.

[ September 27, 2006: Message edited by: wise owen ]
I agree. Here's the link:
subject: Anon Inner Class ??
It's not a secret anymore!