File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
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: 255

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: 9466

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
It's not a secret anymore!