• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

An ambiguous question from round up game

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Beno�t,
Can you, or someone else, please give an example that does illustrate that the point of this question.
Thanks.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Are we home yet? Wait, did we forget the tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic