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 trialSantiago Serrano
2,754 PointsWhy 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...
4 Answers
Lukas Dahlberg
53,736 PointsI 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)
Santiago Serrano
2,754 PointsThanks!
Ignacio Martin Elias
10,127 PointsHe 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
2,754 PointsYup, 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!
Anthony Albertorio
22,624 PointsI'd take a look at this post: https://stackoverflow.com/questions/18194139/make-immutable-java-object
Tonnie Fanadez
UX Design Techdegree Graduate 22,796 PointsWhen 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.
nissen
9,130 Pointsnissen
9,130 PointsDon'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.