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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
ETS User

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

Post 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);
}
}
}

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

What if Inner also has int x?

Guest

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

Post by Guest »

No Inner doesn't have x...
This program is from K & B book....

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.

devlam
Posts: 50
Joined: Sun Nov 10, 2013 4:39 am
Contact:

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

Post by devlam »

This discussion is showing up with question 6.2.35. Wrong link

Post Reply

Who is online

Users browsing this forum: No registered users and 9 guests