• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

final local variables

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the following chunk 'o code:

a compile error is generated: "local variable tc is accessed from within inner class; needs to be declared final."
This is all well and good. I declare tc final, the compile error goes away and the code executes exactly as I would have expected it to if tc was not final.
But I don't understand what declaring tc final actually did. This chunk of code sits inside of a big for-loop so tc ends up holding lots of different references.
I always thought final meant ... well, final. A constant that is declared to have only one value, ever.
I can only assume that this is different because the variable has local scope ... but why?
Anybody know?
- Joe
p.s. How do people like to format their anonymous inner classes?

[This message has been edited by Joe Von (edited July 24, 2001).]
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Joe
The logic behind only allowing local inner classes (classes defined in a method) to access final variables is because when you create an instance of that class there is a very good chance that it will beyond the scope of the method in which it is created. So if you tried to access a non-final variable of the method after the method had gone out scope there is no way to reference it. On the other hand, if the variable is final the compiler can copy its value into the local class so it can be accessed any time.
hope that helps
Dave
 
Joe Von
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your answer Dave!
Let me just see if I understand:
When you declare a local variable final you are flagging the variable for the compiler to say, "Ok I should take the value of this variable as it is right now and include it in the scope of any Inner Classes that reference it. The value might change latter, but if it does I won't go back and tell instances of that IC about it. As far as IC instances are concerned it is final. But it can still change within the scope of the method."
Is that right?
Does the final declaration have any other effect on behavior? If not, why aren't all local variables treated this way by default ... is there a memory hit?
(Typical me, ask one question, get an answer, ask two more.)

- joe
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Joe Von:
Thanks for your answer Dave!
The value might change latter, ... But it can still change within the scope of the method."


When you make it final it can't change later. Its final so once it's been assigned a value it wont change. It can have a different value each time the method is called but once it has been assigned it wont change.


(Typical me, ask one question, get an answer, ask two more.)


Me too

 
Joe Von
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got even more curious and poked about a bit:
Consider the following test code ripped off from "Java in a Nutshell" (and modified to use an anonymous inner class):

This prints out:
0
1
2
3
4
5
6
7
8
9
So the final variable 'fi' actually is still variable within the scope of the main method ... To quote from Nutshell, "each instance of a local class has an automatically created private copy of each of the final local variables it uses, so, in effect it has its own private copy of the scope that existed when it was created."
Which is inherently groovy ... plus it explains the actual behavoir of the program that got me thinkin' 'bout this (always comforting)
Cheers,
Joe
 
reply
    Bookmark Topic Watch Topic
  • New Topic