Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Generating equals and hashcode in intellij

It seems like after generating some equals and hashcode in intellij the private memebers are accessed. I don't understand why this is possible. eg : this is a tidbit of the generated code where it accesses private members.

if (mArtist != null ? !mArtist.equals(song.mArtist) : song.mArtist != null) return false;

mArtist is a private member of song. So it seems like song should not be able to get to this without using a getter.

2 Answers

Check these two awesome Stack Overflow Answers

http://stackoverflow.com/questions/1581478/why-can-i-access-my-private-variables-of-the-other-object-directly-in-my-equa

http://stackoverflow.com/questions/21900632/why-can-a-private-method-be-accessed-from-a-different-instance

Yes, the main method belongs to class A, but it is not accessing the private method from inside the current object in the context of the "this" reference. That doesn't matter. That's just not the model of accessibility that Java uses. What's important is the class in which the code is written, not whether it's accessing members in the same object.

This is very useful, as otherwise (for example) it would be impossible to implement an equals method using private members of both classes.

This may not be how you would have chosen to specify the language, but it is how Java is specified. See JLS 6.6.1 for details of accessibility. In particular:

Otherwise, if the member or constructor is declared private, then access is permitted if and only if it occurs within the body of the top level class (§7.6) that encloses the declaration of the member or constructor. Note that protected access is slightly odd here - a protected instance member of class SuperClass can only be accessed within code in SubClass via an expression of either SubClass or a further subclass. But it still doesn't have to be "the same" object.

I think I understand better now. The private modifier does not affect instances inside the same class. So if an instance of a class (separate from this) is within the same class it was defined the fields can be accessed without public getters and setters.

Cristina Rosales
Cristina Rosales
10,929 Points

Can either of you please dumb this down? Preferably with pictures or something? I really don't get this. Even with the ternary operator ultimate it's returning false, correct? So what is it even evaluating anything for?

Update, setting it up to read

if (!Objects.equals(mArtist, song.mArtist)) return false

makes so much more sense...is there any advantage to the way IntelliJ spits it out at you at the start?