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 trialfediesonlandicho
17,818 PointsWhat does he mean at 3:05?
So the code adds "final" to TextView. I get what that does.
So then Ben talks about "Our onClick method must refer to the same view as long as it exists We can't make it update a different text view by reassigning factLabel to something else."
I'm not sure why making the TextView "final" would make any difference.
Can someone explain further what he means by this and what the reasoning behind it is?
2 Answers
Lucas Smith
11,616 PointsWhat adding the "final" keyword does, is make it so that the value held by the variable can never be changed by another part of the code. So you couldn't assign the variable to a specific view, and then make a method call that would change the view, e.g.
// assign a final int to the number 5
final int number = 5;
/* when attempting to increment the number, it will cause an error
since the value of a final variable cannot be changed*/
number++;
I hope that this helps, -Lucas
Stephen Goodwin
5,479 PointsThe method he is calling that var from needs that var to be final. Ben is just telling you why you can just add the keyword final to that var instead of making a whole new var just for that method.
John Yılmaz
1,713 PointsJohn Yılmaz
1,713 PointsI understand what final key word does, but why we need it unmodifiable.