Originally posted by Joyce Lee:
As mentioned earlier, it's more appropriate to declare constants at the top of a class instead of inside a method. By declaring a variable final means its value cannot be modified once it's assigned. A static variable is only created once per class during loading. A local variable of a method is created each time the method is invoked. See also VM specification: 2.5 Variables for details.
So what is the difference between the following two declarations?
In approach 1, the variable ONE is only created once when the Test class is loaded because it's declared as static. As it's final, the value cannot be changed once it's assigned.
In approach 2, a (local) variable ONE is created each time aMethod is invoked. As it's final, a value can only be assigned once to ONE inside aMethod().
As we can see, the obvious difference is the way variable ONE is created in both approaches.
[ July 30, 2005: Message edited by: Joyce Lee ]