File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Mock Exam Errata and the fly likes error in question number 208 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "error in question number 208" Watch "error in question number 208" New topic
Author

error in question number 208

Manjiri Purandare
Greenhorn

Joined: Mar 14, 2001
Posts: 4
208 in the Round-Up is as follows
can one object access a private variable of another object of the same class?
Answer given by the site is :
Yes.
Reason: Private means "private to the class" and not "private to the object"

According to me, the above answer is wrong. The access modifier 'private' means 'private to the object'.
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi,
Private means --> private to the class!
If you get no or yes answers you only need to figure out 1 case where it is true and the answer is YES. The code below shows one true.

All instances (objects) will be able to change the PRIVATE class variable ... therefore in the above case the statement:
Can one object access a private variable of another object of the same class?
will be Yes!!
Regards,
Manfred.
Manjiri Purandare
Greenhorn

Joined: Mar 14, 2001
Posts: 4
Hi,
I am answering my own question.
Try out this code, which shows that private data is "private to class" and not "private to object".
public class Test
{
private String privateStr;

public Test()
{
privateStr = "Original String";
}

public void method1()
{
Test object2 = new Test();
object2.privateStr = "New String";
// Note private data is accessed using dot operator
System.out.println("method1() prints : " + object2.privateStr);
}

public void method2(Test object3)
{
// Note private data is accessed using dot operator
System.out.println("method2() prints : " + object3.privateStr);
}

public static void main(String args[])
{
Test object1 = new Test();
object1.method1();

Test object3 = new Test();
object1.method2(object3);


}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: error in question number 208