Page 1 of 1

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

Posted: Thu Feb 27, 2014 7:47 pm
by aruna1982

Code: Select all

Consider this class:

class A
{
  private int i;
  public void modifyOther(A a1)
  {
    a1.i = 20;  //1
  }
}

State whether the following statement is true or false:

At //1 a1.i is valid.
Explanation said "Private means private to the class and not to the object."

Isn't this question wrong?

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

Posted: Thu Feb 27, 2014 8:33 pm
by admin
No, it is correct. Why do you think it is wrong?

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

Posted: Thu Feb 27, 2014 9:01 pm
by aruna1982
admin wrote:No, it is correct. Why do you think it is wrong?
Okay, I tested the code and it's working. Could you please explain in more details the statement "Private means private to the class and not to the object." ? As "a1" member is private, it should not accessible from object reference.Isn't it?

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

Posted: Thu Feb 27, 2014 9:29 pm
by admin
That is why the explanation says that private means private to the class, not to the object. You can access i anywhere in class A even if the reference used to access i points to a different instance of class A.

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

Posted: Thu Feb 27, 2014 9:35 pm
by aruna1982
admin wrote:That is why the explanation says that private means private to the class, not to the object. You can access i anywhere in class A even if the reference used to access i points to a different instance of class A.
Thank you very much for the prompt response. This tool helps a lot to improve the knowledge as it explanations. Keep the awesome work. It's very helpful you guys can add more questions.
Thanks.