aspose file tools*
The moose likes Beginning Java and the fly likes Can a final variable be changed at runtime Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can a final variable be changed at runtime" Watch "Can a final variable be changed at runtime" New topic
Author

Can a final variable be changed at runtime

kaffo lekan
Ranch Hand

Joined: Feb 13, 2001
Posts: 42
Hello
i need to know if a final variable can be changed at runtime.
Yes/No , please explain more.
Eric Edwards
Ranch Hand

Joined: Feb 12, 2000
Posts: 60
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.
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

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.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
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://aspose.com/file-tools
 
subject: Can a final variable be changed at runtime