• 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:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

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.



 
Rancher
Posts: 3312
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: 61703
193
  • 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
Rancher
Posts: 3312
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!