I am reading from a file and setting values based on what I read from the file.
My question is: If I wanted to declare one of the retrieved values from the file as a constant how would i do that?
Edit: Say that value is an "int" for simplicity.
I don't suppose you're looking for the final
keyword?
final int foo = /* get it from the file */;
Not sure what scope you want for this variable. The "final" keyword is about all you have to work with, as far as creating constants. It's easy enough to define a final local or instance variable in terms of runtime data, but declaring a static final class member is harder; you have to have the value available right when the class is loaded and initialized, so you have to do it somehow in a static initializer block:
public static final int CONSTANT;
static {
CONSTANT = <something!>;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With