Page 1 of 1

About Question com.enthuware.ets.scjp.v6.2.354 :

Posted: Wed Jul 27, 2011 3:36 pm
by ETS User
Which statements concerning the relation between a non-static inner class and its outer class instances are true?
b.Member variables of the outer instance can always be referred to using only the variable name within the inner instance.
The answer b is correct according to the following program.

class Outer{
private int x=7;
class Inner{
public void seeOuter(){
system.out.println("Outer x is" +x);
}
}
}

Re: About Question com.enthuware.ets.scjp.v6.2.354 :

Posted: Wed Jul 27, 2011 5:15 pm
by admin
What if Inner also has int x?

Re: About Question com.enthuware.ets.scjp.v6.2.354 :

Posted: Thu Jul 28, 2011 4:01 pm
by Guest
No Inner doesn't have x...
This program is from K & B book....

Re: About Question com.enthuware.ets.scjp.v6.2.354 :

Posted: Thu Jul 28, 2011 7:06 pm
by admin
I realize that in this code Inner doesn't have x. But what if it had? Can you access outer class's x directly? No, you cannot. Hence, the option "Member variables of the outer instance can always be referred to using only the variable name within the inner instance." is not correct.

The explanation provided with this option also makes it clear that it is possible only if that variable is not shadowed by another variable in the inner class.

Re: About Question com.enthuware.ets.scjp.v6.2.354 :

Posted: Mon Jan 20, 2014 1:57 pm
by devlam
This discussion is showing up with question 6.2.35. Wrong link