About Question enthuware.ocpjp.v8.2.1469 :

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

Moderator: admin

Post Reply
vonvon
Posts: 2
Joined: Sat Jan 23, 2016 10:48 am
Contact:

About Question enthuware.ocpjp.v8.2.1469 :

Post by vonvon »

I don't understand the explanation of the answer of this question:
Question:
Consider this class:

Code: Select all

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.
Answer:
True
Explanation:
Private means private to the class and not to the object. In other words, members marked private can't be accessed by code in any class other than the class in which the private member was declared.

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

Re: About Question enthuware.ocpjp.v8.2.1469 :

Post by admin »

Since i is an instance variable of class A, it means, each instance of A has its own copy of i. Even though it is declared private, Java allows you to access it from anywhere in class A if you have a valid reference to any object of class A.

In the given code, the modifyOther method is executes in context of one instance of A but is trying to access the variable i of some other instance of A. It is valid because i is not private to an instance. It is private to a class.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

vonvon
Posts: 2
Joined: Sat Jan 23, 2016 10:48 am
Contact:

Re: About Question enthuware.ocpjp.v8.2.1469 :

Post by vonvon »

Now, it's really clear. Thank you very much.

Post Reply

Who is online

Users browsing this forum: No registered users and 38 guests