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
Re: About Question com.enthuware.ets.scjp.v6.2.354 :
Posted: Thu Jul 03, 2025 7:42 am
by nkaragulov
Then why first option is correct?
>>>Member variables of the outer instance are always accessible to inner instances, regardless of their accessibility modifiers.
Same here may be shadowing.
Re: About Question com.enthuware.ets.scjp.v6.2.354 :
Posted: Thu Jul 03, 2025 10:16 pm
by admin
Option 1 and 2 are worded very differently.
Option 1 is correct because it is talking about accessibility and not about how to access it. Even if the variable is shadowed, it is still accessible.
Option 2 is talking about how to access i.e. "referred to using only the variable name". This is not correct because if the variable is shadowed, it cannot be accessed in this manner.