aspose file tools*
The moose likes Beginning Java and the fly likes final local variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "final local variables" Watch "final local variables" New topic
Author

final local variables

Joe Von
Ranch Hand

Joined: Feb 22, 2001
Posts: 32
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).]
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
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


Dave
Joe Von
Ranch Hand

Joined: Feb 22, 2001
Posts: 32
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

Joined: May 10, 2001
Posts: 3244
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

Joined: Feb 22, 2001
Posts: 32
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: final local variables