This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
Originally posted by kaffo lekan: Hello i need to know if a final variable can be changed at runtime. Yes/No , please explain more.
Declaring a variable as final forbids modification of the variable at runtime. Any attempt to modify the value of a final instance variable after it is initialized is a syntax error. Hope this helps.
This is great! Double posts in the forum on a yes/no question and it gets a yes in one and a no in the other! Unfortunately, this is the wrong answer... the answer in your other post ( http://www.javaranch.com/ubb/Forum33/HTML/001682.html ) is correct... -Nate
-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
The fact that a final variable can be initialized within it's constructor gives you the possibility of taking in a parameter at the command line and using that to set the final variable. Be aware though that if you defer initializing a final variable, then it has to be initialized in EVERY ONE of the classes constructors, and some classes have many overloaded constructors. In addition if you have a static final variable, it can be set in a static initializer block. Lastly, if you have a local final variable (like inside a method) it can defer it's initializaion, allowing the value to be passed to the method at runtime. But aside from these "construction" exceptions, Eric is correct, once initialized it can never be modified.
"JavaRanch, where the deer and the Certified play" - David O'Meara
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.
subject: Can a final variable be changed at runtime