Initialize final variable before constructor in Java
Solution 1
I do not really understand your question. That
public class Test3 {
private final String test = "test123";
public Test3() {
System.out.println("Test = "+test);
}
public static void main(String[] args) {
Test3 t = new Test3();
}
}
executes as follows:
$ javac Test3.java && java Test3
Test = test123
Solution 2
Do the initialization in the constructor, e.g.,
private final String name;
private YourObj() {
name = "a name";
}
Of course, if you actually know the value at variable declaration time, it makes more sense to make it a constant, e.g.,
private static final String NAME = "a name";
Solution 3
We're getting away from the question.
Yes, you can use a private final
variable. For example:
public class Account {
private final String accountNumber;
private final String routingNumber;
public Account(String accountNumber, String routingNumber) {
this.accountNumber = accountNumber;
this.routingNumber = routingNumber;
}
}
What this means is that the Account class has a dependency on the two Strings, account and routing numbers. The values of these class attributes MUST be set when the Account class is constructed, and these number cannot be changed without creating a new class.
The 'final' modifier here makes the attributes immutable.
Solution 4
Marking it static, will allow you to use it in the constructor, but since you made it final, it can not be changed.
private static final String name = "a_name";
is is possible to use a static init block as well.
private static final String name;
static { name = "a_name"; }
If you are trying to modify the value in the constructor, then you can't assign a default value or you have to make it not final.
private String name = "a_name";
Foo( String name )
{
this.name = name;
}
or
private final String name;
Foo( String name )
{
if( s == null )
this.name = "a_name";
else
this.name = name;
}
Solution 5
Another possiblity is to initialize the field in an instance initializer blocK:
public class Foo {
final String bar;
{
System.out.println("initializing bar");
bar = "created at " + System.currentTimeMillis();
}
public Foo() {
System.out.println("in constructor. bar=" + bar);
}
public static void main(String[] args) {
new Foo();
}
}
Tobias
Updated on April 28, 2020Comments
-
Tobias about 4 years
Is there a solution to use a final variable in a Java constructor? The problem is that if I initialize a final field like:
private final String name = "a name";
then I cannot use it in the constructor. Java first runs the constructor and then the fields. Is there a solution that allows me to access the final field in the constructor?
-
Tobias about 15 yearsa way without a static field?
-
sfossen about 15 years@Tobiask: Why don't you want a static field?
-
user3532201 about 15 yearsIt's immutable, so you might as well make it static.
-
Brett about 15 yearsTechnically it is the constructor that is (implicitly) executing the field initialisation logic.
-
soulmerge about 15 yearsWell, ok. The fields get evaluated first nonetheless.
-
DJClayworth about 15 yearsBeware of this construct if getName() does anything other than return a constant. You can find the logic used by getName() may not have been initialized.
-
user3532201 about 15 yearsIt's immutable. There's no shared mutable state, thus no concurrency issues. You'd be right if it didn't have that final on it.
-
Steve Gelman over 12 yearsBasically, you get one shot at setting the values in the constructor. After the constructor completes, the attributes are constant.
-
Martin Konicek almost 12 yearsI get an error "cannot assign a value to final variable value".