File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Accessing a Private Variable.

 
Mohit Agarwal
Ranch Hand
Posts: 88
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can one object access a private variable of another objet of the same class?
This was on the rules game .
The ans is yes. The reason is private to the class and not to the object so two objects can access each others private data.

I am confused pls clarify it.

Mohit Agarwal.
Would Be SCJP.


The will to win is worthless if you do not have the will to prepare.
 
David Ulicny
Ranch Hand
Posts: 724
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post some code example? I don't understand what you mean.
 
Colin Fletcher
Ranch Hand
Posts: 200
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lets use the following object as an example:




Let's look at the equals method above. If the obj reference is null (no object) no match.. return. If the obj is an instance of person or the object passed in is a person then we compare.

Since I am the person object (this class file) I know how to compare Person objects. We are the "same" class, therefore I am able to access the private members.

Now, if we had Employee extend Person, Exmployee would NOT be able to see the private members. Why? Because Employee is NOT a person. Employee extends a Person.

Hope this helps.
 
Jay Pawar
Ranch Hand
Posts: 411
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had the same question long time back.. Check out the link below

Here

Hope that helps
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic