This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes private variable scope Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "private variable scope" Watch "private variable scope" New topic

private variable scope

Salil Vverma
Ranch Hand

Joined: Sep 06, 2009
Posts: 253

The code attached below works fine and prints true which is fine but I could not understand why there was no compilation error on the equals function while it was trying to access the private member of another object. Could you please help me understanding this,

Salil Verma
Ankit Garg

Joined: Aug 03, 2008
Posts: 9280

The equals method is accessing private member on an instance of its own class. This is allowed. The members of the class have access to private members of objects of their own class...

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
suman mitragupta

Joined: Jul 23, 2009
Posts: 11
private varibale with object instance is ok.

Java Architect
scjp 1.5 sample questions scjp 1.6 sample questions
Atwal Usha
Ranch Hand

Joined: Sep 10, 2009
Posts: 137
Well, the private member is used within the class and it is allowed. There would have been compiler error if it would have been used outside the class.

Java Certification Exam Mock Tests: SCJA  SCJP 5  SCJP 6  SCJP 6 (Online Training)  SCJP 6 (Instructor Led Training)   SCWCD 5   SCBCD 5  SCEA 5
I agree. Here's the link:
subject: private variable scope
Similar Threads
problems from examlab
Understanding hashcode()
Wrong Answer in ExamLab SCJP 1.5
Using PipedInputStream and PipedOutputStream with Threads
equals method explanation needed regarding this question