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!

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 Objects Meet Objects Final

Ethan Morris
Ethan Morris
19,055 Points

using public final instead of private final for characterName?

Now that we have learned about the final modifier is it not better to use it along with public for the characterName variable? e.g. "public final String characterName;"

This way the property can still be access with dispenser.characterName instead of having to create a getter method to return it. The final modifier achieves the intended purpose of making characterName private in an earlier video which was to stop it being updated once created.

1 Answer

Traian Butaru
Traian Butaru
5,451 Points

I think you get the same thing, but you’re missing on benefits of the encapsulations. Maybe, at a later date, you would like to do some manipulations before returning the characterName. It’s easier just to modify the getter than creating an extra method and changIng all the code to work with your new method.