Vijendran Selvarajah
Vijendran Selvarajah

Reputation: 1350

Is there a way to convert a variable to a constant in java?

I want to set the value for a spring annotation at the runtime by reading the property file. I can read the value form the property file and assign to a variable, but annotations only accepts "constants", **is it possible to change this variable to constant ** and use it as the value for the annotation ?

So as I already explain "Is it possible to convert a variable to a constant in java"?

Upvotes: 4

Views: 2910

Answers (1)

vapiri
vapiri

Reputation: 32

You can't convert a variable to a constant, but, depending on the scope, you can create a new final variable and initialise it with the non-const variable. This worked for me both inside a method and in the class itself

int test = 10;
final int test2 = test;

Upvotes: 1

Related Questions