Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Rule Round Up: Question 80

 
Anatol Bollinger
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone.

The question #80 of the Java Rule Round-up is:

"Can one object access a private variable of another object of the same class?"

The answer is Yes, with the explanation that private means "private to the class" and NOT "private to the object".

Is that correct? I thought an object cannot (directly) access its own private variables even, how should it be able to access the private variables of another object?

Do I misunderstand the question?

Thanks for your help,
Anatol
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Boi, if an object couldn't access its own private data, we are in big trouble

If you don't believe what question 80 claims, write some code to test
your suspicions. It should only take a minute and you'll learn from the
exercise! Test before you post...
[ November 10, 2005: Message edited by: Jeff Albrechtsen ]
 
Rob Spoor
Sheriff
Pie
Posts: 20529
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Private members are implementation details which only the class itself knows about. Any instance knows this, and because all instances use the same code within a JVM they know all other instances share the same implementation, and hence the same private members.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good reasoning, but ultimately, it is that way by fiat. In Smalltalk, you
might say that fields are all protected, since a subclass can directly access
superclass fields, but one instance of X cannot access any fields in another instance of X. That's just how the language is defined.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Anatolb"-
Welcome to the JavaRanch! Please adjust your displayed name to meet the

JavaRanch Naming Policy.

You can change it

here.

Thanks! and welcome to the JavaRanch!

Mark
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically private would mean that an instance of an object has its own values of the instance variables, and that instance is the only object that can access them directly.

Mark
 
Marilyn de Queiroz
Sheriff
Posts: 9063
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you positively certain of that, Mark? I think I can write some code that proves otherwise.
 
Anatol Bollinger
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Jeff: I actually did what you suggested and wrote some code to test it, before I wrote the post. That's why I assume that I misunderstood the question and/or the answer.

Example:



This can be accomplished using the setName() method of course, but that means accessing a (for this example) public method and not the private variable, no?

@Marilyn: Could you give an example of one object accessing private variables of another object? I am curious!

Anatol (aka anatolb )
 
Sowjanya Chowdary
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's interesting.
I too wonder, how an object can access its private variables,
and another object's too.

Jeff and Marilyn , Can you please help beginners like me to understand that concept?
 
Rob Spoor
Sheriff
Pie
Posts: 20529
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This compiles perfectly, although value is private.
 
Anatol Bollinger
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Rob: You're right and I am quite :flabbergasted:

I was not aware of that and suddenly the expression "private to the class" makes a lot of sense!

Thanks for the enlightenment,
Anatol
 
Marilyn de Queiroz
Sheriff
Posts: 9063
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Two different House objects where one House object is accessing the private variable of the other House object.
 
Vijay Raj
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marilyn:

Where is one House object is accessing the private variable of the other House object? Is it in this line:

>> houseB.numberOfChairs = houseA.numberOfChairs;

But we are simply assigning integer values, is an object accessing another object?

This thread has been helpful. All this time I supposed that object cannot access private members directly.

regards,
vijay.
 
Rob Spoor
Sheriff
Pie
Posts: 20529
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vijay R.:
Marilyn:

Where is one House object is accessing the private variable of the other House object? Is it in this line:

>> houseB.numberOfChairs = houseA.numberOfChairs;

But we are simply assigning integer values, is an object accessing another object?
It is assigning integer values yes. However, these integer values are private members (variables) of two different objects. Here it is not an object accessing the private variable but a class accessing the private variables of instances of that same class.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic