A source I am reading says that the keyword private
means a method or variable is private at the class level, not the object level.
Meaning in a chunk of code like this:
public class Weight2 implements Comparable<Weight2>
{
private int myPounds, myOunces;
public Weight2()
{
myPounds = myOunces = 0;
}
public Weight2(int x, int y)
{
myPounds = x;
myOunces = y;
}
public int compareTo(Weight2 w)
{
if(myPounds<w.myPounds)
return -1;
if(myPounds>w.myPounds)
return 1;
if(myOunces<w.myOunces)
return -1;
if(myOunces>w.myOunces)
return 1;
return 0;
}
}
A Weight2 object can access the private fields of a different weight2 object without an accessor method... but rather by just saying w.myPounds
.
CLARIFICATION:
I want to know from where objects can access a different object's private data. Is it only from within the class? Or could this be done from a driver program?