You can also use Forward Reference initialization
public class ForwardReference {
private static final int i = getValue();
private static final int j = 2;
public static void main(String[] args) {
System.out.println(i);
}
private static int getValue() {
return j*2;
}
}
The key point here is that we get the value of 'j' from 'getValue' before declaring 'j'. Static variables are initialized so that they are displayed.
This will print the correct value "4"
source
share