Someone explain to me the differences between the following two statements?
A static final
variable initialized by a static
code block:
private static final String foo;
static { foo = "foo"; }
A static final
variable initialized by an assignment:
private static final String foo = "foo";
In this example, there's one subtle difference - in your first example, foo
isn't determined to be a compile-time constant, so it can't be used as a case in switch
blocks (and wouldn't be inlined into other code); in your second example it, is. So for example:
switch (args[0]) {
case foo:
System.out.println("Yes");
break;
}
That's valid when foo
is deemed to be a constant expression, but not when it's "just" a static final variable.
However, static initializer blocks are usually used when you have more complicated initialization code - such as populating a collection.
The timing for initialization is described in JLS 12.4.2; any static final fields which are considered as compile-time constants are initialized first (step 6) and initializers are run later (step 9); all initializers (whether they're field initializers or static initializers) are run in textual order.
See more on this question at Stackoverflow