• Post Reply Bookmark Topic Watch Topic
  • New Topic

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: 2982
32
  • 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: 58831
179
  • 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: 2982
32
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!