wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes An ambiguous question from round up game Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "An ambiguous question from round up game" Watch "An ambiguous question from round up game" New topic
Author

An ambiguous question from round up game

Bin Zhao
Ranch Hand

Joined: Oct 04, 2000
Posts: 73
Can one object access a private variable of another object of the same class?
answee is yes.
But I think that is only right when you access the private variable in the same class.Consider the following code:
class A{
private int i=0;
}
public class Tester{
public static void main(String args[])
{
A a1=new A();
A a2=new A();
a1.i=a2.i+1;
System.out.println("a1\'s i is "+a1.i);
}
}
above code won't compile.
So I think the answer is not always yes.
Anyone may correct me?
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi Bin,
The code you give does not illustrate at all an object accessing a private variable of another object of the same class... instead, the class accessing <code>a2.i</code> is the Tester class, not class A.
Cheers,
Beno�t
Santosh Jaiswal
Greenhorn

Joined: Oct 04, 2000
Posts: 26
In IMO, Any object can access any private member of its own class. So in this case, it is always "yes".
Correct me if I am wrong?
Thanks
Rob Levo
Ranch Hand

Joined: Oct 01, 2000
Posts: 167
Beno�t,
Can you, or someone else, please give an example that does illustrate that the point of this question.
Thanks.
Mary Chaddi
Greenhorn

Joined: Nov 01, 2000
Posts: 7
Its some thing like this. Am I right guys?

<pre>
class Rectangle{
private int length , width;
public void setSize(Rectangle o , int l , int w)
{
o.width = w;
o.length = l;
}
public int getArea()
{
return width * length;
}
}
public class Test{
public static void main(String[] args)
{
Rectangle a = new Rectangle();
Rectangle b = new Rectangle();
a.setSize(b , 5 , 10);
System.out.println(b.getArea());
}
}
</pre>

Rgds
MC

[This message has been edited by Mary Chaddi (edited November 04, 2000).]
Bob Lulyk
Greenhorn

Joined: Nov 05, 2000
Posts: 5
Alternately, the following illustrates the concept as stated on
page 99 of the Java Language Specification ("if the member or
constructor is declared private, then access is only permitted
when it occurs from within the class in which it is declared"):
*****************************************************************
class OtherClass
{
private int otherClassInt_ = 1;
}

class SameClass
{
private int sameClassInt_;

SameClass(int sameClassInt)
{
sameClassInt_ = sameClassInt;
}
void showSameClassInt(SameClass sameClassInstance)
{
System.out.println(sameClassInstance.sameClassInt_);
}
//void showOtherClassInt(OtherClass otherClassInstance)
//{
// System.out.println(OtherClass.otherClassInt_);
//}
}

class test
{
public static void main(String[] args)
{
SameClass instance1 = new SameClass(1);
SameClass instance2 = new SameClass(2);
instance2.showSameClassInt(instance1); // DISPLAYS "1"
instance1.showSameClassInt(instance2); // DISPLAYS "2"
}
}
This example compiles and executes properly only if method
"showOtherClassInt" is commented out; if it is not commented out, then you receive the error message "otherClassInt_ has private access in OtherClass" upon compilation.



Bin Zhao
Ranch Hand

Joined: Oct 04, 2000
Posts: 73
Sorry,I have misunderstood the meaning of "one object access a private variable of another object of the same class".
Now I see what is the point.
Thanks all.
 
 
subject: An ambiguous question from round up game
 
Similar Threads
exam lab gc question
Question from http://www.javaranch.com/game.html...
access private member
Error in the SCJP6 Study Guide Mock Exam Question?
synchronize variable by lock