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 Java Data Structures Efficiency! Custom Serialization

Comparing Song Titles. Why not use getTitle()?

At about 14:20, when fixing the getSongsForArtist method to sort the songs alphabetically, why did we change the member variables to be protected instead of just using song1.getTitle()/song2.getTitle() ?

1 Answer

Craig Dennis
Craig Dennis
Treehouse Teacher

Honestly, for two reasons.

First, and not satisfying, I needed a way to introduce the protected keyword. You don't use it often, but when you see it, I want you to know why it is used.

Secondly, getter methods can have other associated tasks in them, sometimes accessing the instance variable directly is preferred. Sometimes using a getter will cause a db hit and is in fact what you want to have happen. So it is contextual, but I wanted to show the concept, albeit blurry. :/ Sorry.

Hope that helps!

Thank you!