Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.
Dillon Shaw6,400 Points
Why does Android Studio make the latitude and longitude final?
The video says it's because we use them in the anonymous inner class but I don't see why that necessitates their being final.
Anonymous inner classes require final variables because of the way they are implemented in Java. An anonymous inner class (AIC) uses local variables by creating a private instance field which holds a copy of the value of the local variable. The inner class isn’t actually using the local variable, but a copy. It should be fairly obvious at this point that a “Bad Thing”™ can happen if either the original value or the copied value changes; there will be some unexpected data synchronization problems. In order to prevent this kind of problem, Java requires you to mark local variables that will be used by the AIC as final (i.e., unchangeable). This guarantees that the inner class’ copies of local variables will always match the actual values.
Sachin Jayaram5,461 Points
The latitude and longitude are hard coded and for this example shouldn't be changed, hence the final keyword. If you automate the process of finding the latitude and longitude, final keyword should be removed.
Scott Junner9,010 Points
It's also a good idea to experiment a little and see if you can break it. Remove the final keyword and see what happens. See what errors or warning AndroidStudio produces. See what build or run time errors are produced. Then just put it back once you feel like you've gained something from the experience.