Java Java Objects Meet Objects Final

Ethan Morris
Ethan Morris
17,636 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.