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 Objects (Retired) Meet Objects Privacy and Methods

Aditya Puri
Aditya Puri
1,080 Points

Another approach

I understand that the goal of this video is to familiarize us with objects..

But can't we do the same thing by doing

public final String mCharacterName = "Yoda";

I just want to know if this is correct too or if this way has some limitations against using the other way in which we first declare a method..

1 Answer

Hello, Aditya!

While this could work for some scenarios, it would limit you on cases like the one in the video where you want to be able to actually modify mCharacterName, just simply not allow any other object to do it. By declaring it as final you're essentially saying that the value will not change and remain the same throughout the life of the instance.

It's also good practice in general to not expose the components of a class directly but rather through a method since it gives you greater flexibility in case you wanted to change some behaviour in your class; like wanting the name to always be presented in uppercase in the future.