Initialize final variable before constructor in Java

constructor, final, initialization, java, variables

Solution

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

Problem

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?

Original source