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

Android Build a Simple Android App (retired 2014) Getting Started with Android Adding an OnClickListener to a Button

Why final is only added for TextView?

Hi,

Why final is required only for TextView but not Button view?

Thanks

2 Answers

Calvin Nix
Calvin Nix
43,828 Points

Hey Baris,

"The Onclick method must refer to the same method as long as it exists. You can't make it update a different textview by reassigning the answerlabel variable to some other textview"

Final makes sure that the variable always points to the object that it was first assigned to. If you try to change this then you will receive errors.

Hi Calvin,

So onclick requires all variables declared outside the function to be final, right?

Calvin Nix
Calvin Nix
43,828 Points

Hey Baris,

Pretty much. It just protects the variable from be re-assigned.

Eclipse will let you know if it is unhappy with something though. :)