This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Journey To Enterprise Agility and have Daryl Kulak & Hong Li on-line!
See this thread for details.
Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

location variable accessed from inner class needs to be declared final  RSS feed

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am listening user location and trying to do a distance calculation based on a longitude latitude value that i am pulling from database.The problem is i cannot access location.getLatitude() because compiler says me that i need to declare location final.But since its changing i cant do that.



 
Master Rancher
Posts: 3260
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

compiler says me that i need to declare location


Have you tried that?  What happened?
Where in the posted code is the value of location (the arg to the onLocationChanged method) changed?
 
yunus emre güney
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:

compiler says me that i need to declare location


Have you tried that?  What happened?
Where in the posted code is the value of location (the arg to the onLocationChanged method) changed?



I cant write Location final location
 
Marshal
Posts: 59742
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That appears to be Android code, so I shall move you to our Android forum.

Yes, local variables used in a local class, or in Java8+, in a λ must be “effectively final”. In Java7 and apparently in Android too, that means you need to use the final keyword. In Java8+ you can get away with never reassigning the local variable; in fact the compiler error you are seeing wouldn't have occurred in Java8.
Please don't use screenshots to post your code.
 
Norm Radder
Master Rancher
Posts: 3260
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I cant write Location final location


Why not? Please explain what the problem is.

Did you try if?  What happened?


Note: the final keyword goes before the type name: final Location
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!