Why my program doesn't show compile time error when final class variable is not initialized?-Collection of common programming errors

Interestingly enough, the code will compile whether or not the field is marked static – and in IntelliJ, it will complain (but compile) with the static field, and not say a word with the non-static field.

You’re right in that JLS §8.1.3.2 has certain rules regarding [static] final fields. However, there’s a few other rules around final fields that play a large role here, coming from the Java Language Specification §4.12.4 – which specify the compilation semantics of a final field.

But before we can get into that ball of wax, we need to determine what happens when we see throws – which is given to us by §14.18, emphasis mine:

A throw statement causes an exception (§11) to be thrown. The result is an immediate transfer of control (§11.3) that may exit multiple statements and multiple constructor, instance initializer, static initializer and field initializer evaluations, and method invocations until a try statement (§14.20) is found that catches the thrown value. If no such try statement is found, then execution of the thread (§17) that executed the throw is terminated (§11.3) after invocation of the uncaughtException method for the thread group to which the thread belongs.

In layman’s terms – during run-time, if we encounter a throws statement, it can interrupt the execution of the constructor (formally “completes abruptly”), causing the object to not be constructed, or constructed in an incomplete state (which could be a security hole, depending on the platform and partial completeness of the constructor).

What the JVM expects, given by §4.5, is that a field with ACC_FINAL set never has its value set after construction of the object:

Declared final; never directly assigned to after object construction (JLS §17.5).

So, we’re in a bit of a pickle – we expect behavior of this during run-time, but not during compile-time. And why does IntelliJ raise a mild fuss when I have static in that field, but not when I don’t?

First, back to throws – there’s only a compile-time error with that statement if one of these three pieces aren’t satisfied:

  • The expression being thrown is unchecked or null,
  • You try to catch the exception, and you’re catching it with the right type, or
  • The expression being thrown is something that can actually thrown, per §8.4.6 and §8.8.5.

So compiling a constructor with a throws is legitimate. It just so happens that, at run-time, it will always complete abruptly.

If a throw statement is contained in a constructor declaration, but its value is not caught by some try statement that contains it, then the class instance creation expression that invoked the constructor will complete abruptly because of the throw (§15.9.4).

Now, onto that blank final field. There’s a curious piece to them – their assignment only matters after the end of the constructor, emphasis theirs.

A blank final instance variable must be definitely assigned (§16.9) at the end of every constructor (§8.8) of the class in which it is declared; otherwise a compile-time error occurs.

What if we never reach the end of the constructor?

First program: Normal instantiation of a static final field, decompiled:

// class version 51.0 (51)
// access flags 0x21
public class com/stackoverflow/sandbox/DecompileThis {

    // compiled from: DecompileThis.java

    // access flags 0x1A
    private final static I i = 10

    // access flags 0x1
    public ()V
            L0
    LINENUMBER 7 L0
    ALOAD 0
    INVOKESPECIAL java/lang/Object. ()V
            L1
    LINENUMBER 9 L1
            RETURN //  0) {
        return true;
    } else {
        throw new IllegalStateException("Non-natural number!?");
    }
}