• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Can a final variable be changed at runtime

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello
i need to know if a final variable can be changed at runtime.
Yes/No , please explain more.
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
You learn how to close your eyes and tell yourself "this just isn't really happening to me." Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic