aspose file tools*
The moose likes Beginning Java and the fly likes Java Rule Round Up: Question 80 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java Rule Round Up: Question 80" Watch "Java Rule Round Up: Question 80" New topic
Author

Java Rule Round Up: Question 80

Anatol Bollinger
Greenhorn

Joined: Nov 10, 2005
Posts: 11
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

Joined: Sep 16, 2005
Posts: 1780
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 ]

There is no emoticon for what I am feeling!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
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

Joined: Feb 05, 2001
Posts: 17259
    
    6

"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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

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

Joined: Jul 22, 2000
Posts: 9052
    
  12
Are you positively certain of that, Mark? I think I can write some code that proves otherwise.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Anatol Bollinger
Greenhorn

Joined: Nov 10, 2005
Posts: 11
@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

Joined: Aug 22, 2005
Posts: 35
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

Joined: Oct 27, 2005
Posts: 19760
    
  20

This compiles perfectly, although value is private.
Anatol Bollinger
Greenhorn

Joined: Nov 10, 2005
Posts: 11
@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

Joined: Jul 22, 2000
Posts: 9052
    
  12

Two different House objects where one House object is accessing the private variable of the other House object.
Vijay Raj
Ranch Hand

Joined: Oct 10, 2005
Posts: 110
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

Joined: Oct 27, 2005
Posts: 19760
    
  20

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Rule Round Up: Question 80