jQuery in Action, 2nd edition*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why can I access this private property directly? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Why can I access this private property directly?" Watch "Why can I access this private property directly?" New topic
Author

Why can I access this private property directly?

Jack McGuire
Greenhorn

Joined: Jan 24, 2012
Posts: 10
Hi!

From page 546 on the SCJP Study Guide. I seem to be able to access the private property "moofValue" directly in the equals method without having to HAVE TO use the getMoofValue() getter method. Why is this?

Thanks for your help.

Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4344
    
    8

"private" means "accessible only from within the same class". In this example you're accessing it from within the Moof class, so there's no restriction.
Jack McGuire
Greenhorn

Joined: Jan 24, 2012
Posts: 10
Hey Matthew.

This means, I guess, that even when the object involved is not the "this" object, but is an argument object "Moof o" that is passed into the equals method, you can still access the argument "Moof o"s properties directly as we are "inside" the Moof class.

Is that the correct thinking here?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4344
    
    8

That's right. It's restricted to the class, not the object.
Jack McGuire
Greenhorn

Joined: Jan 24, 2012
Posts: 10
Thanks! :-)
Detlef Boehm
Greenhorn

Joined: Sep 14, 2012
Posts: 1
Hi Jack, hi others,
isn't it also, because the overridden equals() method is public!? And it has to be public, regarding the rules for overriding.

Best regards
Det
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4344
    
    8

Hi Detlef. Welcome to the Ranch!

Detlef Boehm wrote:isn't it also, because the overridden equals() method is public!? And it has to be public, regarding the rules for overriding.

No, it's nothing to do with that. You're right that the equals() method has to be public because it's overriding a public method; but the visibility of the method doesn't affect what the code inside that method can access, it only affects whether other code can access that method.
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

Detlef Boehm wrote:Hi Jack, hi others,
isn't it also, because the overridden equals() method is public!? And it has to be public, regarding the rules for overriding.

Best regards
Det


no. private members are accessible from within the class. a method may be public, protected or private, if it is in the same class it can access private members of its class
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why can I access this private property directly?
 
Similar Threads
Not able to understand the program
Want hepl to understand Implementin an equals()
instanceof
instance of problem
doubt in equals method parameter