wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Private Variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Private Variables" Watch "Private Variables" New topic
Author

Private Variables

Amit Lonkar
Greenhorn

Joined: Dec 12, 2001
Posts: 25
The following is the question on the www.javaranch.com/game.jsp. Please can some one tell me how this is possible.
Can one object access a private variable of another object of the same class??
Answer: Yes
Private means "private to the class" NOT "private to the object". So two objects of the same class could access each other's private data.
Thanks
Amit
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Here you go.

Main constructs a new PrivateTest, and is able to access a private member of that class. It also creates a second object of the same class, and passes this as an argument to a method in the first object. The printanInt() method is able to access the private member of another object, because both objects are of the same class.


Rob
SCJP 1.4
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Private Variables
 
Similar Threads
error in question number 208
Private Variables
JavaRanch Rule Round-up Game?
Java Rule Round-Up
can one object access a private variable of another object of the same class?