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 - Retired Getting There Class Review

Santiago Serrano
Santiago Serrano
2,754 Points

Why didn't Craig use the "final" keyword while declaring the Treat member variables?

Hello! I don't understand why didn't Craig use the "final" keyword when declaring the member variables of the class "Treat" if those variables were not supposed to change once initialized by the constructor (I think they were supposed to be constants). If someone could help me understand why, I would appreciate it very much!

UPDATE: In my workspace, I declared the member variables of the class "Treat" as final and the code works as expected. I even ran the getters of those three member variables just in case, and printed them out. They work perfectly...

Don't know if this helps, like I've not watch the video, I think ... He might need a default constructor, because of eventual frameworks requirements which could cause some possible conflict.

4 Answers

I mentioned this is in a different question, but employing the final keyword can cause some confusion to beginners because it has different uses in different contexts. For instance, a final class cannot be extended, but you can still create a new instance of it.

So, you are correct that for any constant, you can use the final keyword without issue. The compiler will also optimize a variable that's not reassigned as 'effectively final' as well. (This concept will show up again if/when you start using lambda functions)

Ignacio Martin Elias
Ignacio Martin Elias
10,127 Points

He could have used the final keyword. However, by defining the variables as private and not giving any public method to set those variables, after creating the object there is no possible way to change the values of the variables from anywhere except from the same class, which isn't the class where we created the object. Hope it helps

Santiago Serrano
Santiago Serrano
2,754 Points

Yup, that's what I was thinking... anyways, I think it would be better to declare them as final since the keyword is meant for variables that won't be changed. Thanks for your reply!

Tonnie Fanadez
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Tonnie Fanadez
UX Design Techdegree Graduate 22,796 Points

When you mark your variable private, that means that this variable should only be available in the certain class, and shouldnโ€™t be directly accessed without a getter or setter method in the class. On the other hand, when you mark a variable final, you are basically saying that the value of the variable is going to be constant, which means that it wonโ€™t change as long as the program runs.