*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes accessing private variable of another object of same class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "accessing private variable of another object of same class" Watch "accessing private variable of another object of same class" New topic
Author

accessing private variable of another object of same class

anil bisht
Ranch Hand

Joined: Nov 04, 2000
Posts: 81
can one object access a private variable of another object of same class...
the answer says yes ... how ???
i saw this answer somewhere in this discussion forum..
so please if someone could answer it or send me the link to that discussion ..
TIA
anil
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
As long as they are object of the same classes and not object of subclasses, I don't see why not. It's easier to fire up your compiler to test these kinds of things. I can't remember where I saw this inthis forum. I believe was a similar question in the "rules round up" game
Bosun


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Sahir Shah
Ranch Hand

Joined: Nov 05, 2000
Posts: 158
Hmmmmm. Can be tricky. I'll give it a shot anyway.
<pre>
class Rectangle{
private int width , length;
public Rectangle(int w , int l)
{
width = w;
length = l;
}
public int getArea()
{
return width * length;
}
private void setSize(int w , int l)
{
width = w;
length = l;
}
public void clone(Rectangle rectangle)
{
rectangle.setSize(this.width , this.length);
}
}
public class SameClassObject{
public static void main(String[] args)
{
Rectangle r1 = new Rectangle(10,10);
System.out.println(r1.getArea());
Rectangle r2 = new Rectangle(20,20);
r2.clone(r1);
System.out.println(r1.getArea());
}
}
</pre>

Cheers
Sahir
oops. sorry. I keep forgetting to close the pre tag.

[This message has been edited by Sahir Shah (edited December 09, 2000).]


....
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: accessing private variable of another object of same class
 
Similar Threads
can one object access a private variable of another object of the same class?
Java Rule Round-Up
incorrect answer
error in question number 208
Private Variables