Difference between static modifier and static block [duplicate]

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.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)