About Question com.enthuware.ets.scjp.v6.2.354 :
Moderator: admin
About Question com.enthuware.ets.scjp.v6.2.354 :
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);
}
}
}
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);
}
}
}
-
- Site Admin
- Posts: 10391
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.354 :
What if Inner also has int x?
Re: About Question com.enthuware.ets.scjp.v6.2.354 :
No Inner doesn't have x...
This program is from K & B book....
This program is from K & B book....
-
- Site Admin
- Posts: 10391
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.354 :
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.
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.
-
- Posts: 50
- Joined: Sun Nov 10, 2013 4:39 am
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.354 :
This discussion is showing up with question 6.2.35. Wrong link
-
- Posts: 20
- Joined: Mon Sep 16, 2024 1:49 am
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.354 :
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.
>>>Member variables of the outer instance are always accessible to inner instances, regardless of their accessibility modifiers.
Same here may be shadowing.
-
- Site Admin
- Posts: 10391
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.354 :
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.
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.
Who is online
Users browsing this forum: Google [Bot] and 4 guests