• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Question from http://www.javaranch.com/game.html...

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Can anybody explain this question.......

Q:Can one object access a private variable of another object of same class?
A:"Yes".Private means "private to class", not "private to object". So two objects
of the same class could access each other's private data.

Say the class structure is..
class A{
private int x;
public static void main(String args[])
{
A a1 = new A();
A a2 = new A();
//code
}
}
If i say a2.x it is specific to a2 object only,right? How can i get the value of x associated with a1 using a2?

Thanx,
Srikant.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It depends whether that private variable is static or instance variable :
public class Class1 {
private static int count = 1;
private int model = 2000;
public static void main(String args[]) {
Class1 c1 = new Class1();
Class1 c2 = new Class1();
c1.count = 2;
System.out.println(c1.count);
System.out.println(c2.count);
c1.model = 2001;
System.out.println(c1.model);
System.out.println(c2.model);
}
}
Output :
2
2
2001
2000
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This code is more clear :
public class Class1 {
private int model = 2000;
public static void main(String args[]) {
Class1 c1 = new Class1();
c1.model = 2001;

Class1 c2 = new Class1();
c2.model = 2002;

c2.Display(c2);
c2.Display(c1);
}
void Display(Class1 c) {
System.out.println(c.model);
}
}

Output :
2002
2001

A private variable or method may only be used by an instance of the class that declares the variable or method.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx Dilip, this cleared my confusion....
Srikant.
 
If you're gonna buy things, buy this thing and I get a fat kickback:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic