This Q rather for verification:
A static final field can be initialized when it's declared:
public static final int i=87;
or in the static block:
public static final int i;
//..........
static {
...
i=87;
...
}
Is there anywhere, other than the static block, a static final field
public static final int i;
can be initialized?
Thanks in advance.
Note: saw Initialize a static final field in the constructor. it's not specific that the static block is the only place to initialized it outside the declaration.
//==============
ADD:
extending @noone's fine answer, in response to @Saposhiente's below:
mixing in some non-static context:
public class FinalTest {
private static final int INT = new FinalTest().test();
private int test() {
return 5;
}
}
It could be "initialized" in any random static method, but only indirectly by using the static
block, or the variable initialization.
public class FinalTest {
private static final int INT = test();
private static int test() {
return 5;
}
}
Or like this:
public class FinalTest {
private static final int INT;
static {
INT = test();
}
private static int test() {
return 5;
}
}
Technically, it is not really an initialization in test()
, but it acts like one.