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 Weather App (2015) Hooking Up the Model to the View Adding a Refresh Button

Dillon Shaw
Dillon Shaw
6,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.

3 Answers

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.

Dillon Shaw
Dillon Shaw
6,400 Points

This makes sense, thank-you!

Sachin Jayaram
Sachin Jayaram
5,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.

Boban Talevski
Boban Talevski
24,793 Points

I don't think you can get away without using the final keyword in this case due to reasons Zaur Kandokhov explained above (and the Android Studio won't let you run the app). About using an automated process to get the latitude and longitude, you can still use the final keyword, you would just get the location information before assigning the values to latitude and longitude.

Now, the app should probably consider that the user/device moves around and should check for a change in location every once in a while. This could be implemented by a new Location class (which will have non final members longitude and latitude), initiating a final object of that class in MainActivity (probably as a member variable?), and use that final object as a parameter to the onClick() method.

According to https://stackoverflow.com/questions/32791995/error-cannot-assign-a-value-to-final-variable and I just tested this out of curiosity, you could update the values of latitude and longitude when needed using setters as those variables are not final, and can get their values using getters inside the onClick() method to make the API call for the right location.

Or, I guess if we somehow recreate the activity whenever we want to check for an updated location, we can keep using the initial simple solution of assigning the final values to latitude and longitude after getting the device's location.

Scott Junner
Scott Junner
9,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.