I manage an open source project and have a user reporting a situation which I think is impossible according to Java's order of initialization of static variables in classes.  The value of a static final class variable is incorrect, apparently resulting from different results of a dependency's static method based on its own static final variable.
I'd like to understand what's happening in order to figure the best workaround. At the moment, I am baffled.
The main entry point for my project is the class SystemInfo which has the following constructor:
public SystemInfo() {
    if (getCurrentPlatform().equals(PlatformEnum.UNKNOWN)) {
        throw new UnsupportedOperationException(NOT_SUPPORTED + Platform.getOSType());
    }
}
When run by itself, the problem doesn't reproduce; but when run as part of many tests being executed a larger build (mvn install) it is consistently reproducible, implying the problem is likely associated with multithreading or multiple forks.  (To clarify: I mean the simultaneous initialization of static members in two different classes, and the various JVM-internal locking/synchronization mechanisms associated with this process.)
They receive the following result:
java.lang.UnsupportedOperationException: Operating system not supported: JNA Platform type 2
This exception implies two things are true when SystemInfo instantiation begins:
getCurrentPlatform() is the enum value PlatformEnum.UNKNOWN
Platform.getOSType() is 2However, this situation should be impossible; a value of 2 would return WINDOWS, and unknown would return a value other than 2.  Since both variables are both static and final they should never simultaneously reach this state.
I have tried to reproduce this on my own and failed, and am relying on a report from a user executing tests in their Kotlin-based (kotest) framework.
The user's MCRE simply invokes this constructor as part of a larger number of tests, running on the Windows operating system:
public class StorageOnSystemJava {
    public StorageOnSystemJava(SystemInfo info) {
    }
}
class StorageOnSystemJavaTest {
    @Test
    void run() {
        new StorageOnSystemJava(new SystemInfo());
    }
}
The getCurrentPlatform() method simply returns the value of this static final variable.
public static PlatformEnum getCurrentPlatform() {
    return currentPlatform;
}
This is a static final variable populated as the very first line in the class (so it should be the first thing initialized):
private static final PlatformEnum currentPlatform = queryCurrentPlatform();
where
private static PlatformEnum queryCurrentPlatform() {
    if (Platform.isWindows()) {
        return WINDOWS;
    } else if (Platform.isLinux()) {
        // other Platform.is*() checks here
    } else {
        return UNKNOWN; // The exception message shows the code reaches this point
    }
}
This means that during class initialization, all of the Platform.is*() checks returned false.
However, as indicated above this should not have happened.  These are calls to JNA's Platform class static methods.  The first check, which should have returned true (and does, if called in the constructor or anywhere in code after instantiation) is:
public static final boolean isWindows() {
    return osType == WINDOWS || osType == WINDOWSCE;
}
Where osType is a static final variable defined thus:
public static final int WINDOWS = 2;
private static final int osType;
static {
    String osName = System.getProperty("os.name");
    if (osName.startsWith("Linux")) {
        // other code
    }
    else if (osName.startsWith("Windows")) {
        osType = WINDOWS; // This is the value being assigned, showing the "2" in the exception
    }
    // other code
}
From my understanding of the order of initialization, Platform.isWindows() should always return true (on a Windows OS).  I do not understand how it could possibly return false when called from my own code's static variable initialization.  I've tried both the static method, and a static initialization block immediately following the variable declaration.
SystemInfo constructorSystemInfo class initialization begins ("T is a class and an instance of T is created.")static final currentPlatform variable is encountered by the initializer (first line of class)queryCurrentPlatform() to obtain a result (same result if the value is assigned in a static block immediately following the static variable declaration)Platform.isWindows() static method is calledPlatform class is initialized ("T is a class and a static method of T is invoked.")Platform class sets the osType value to 2 as part of initializationPlatform initialization is complete, the static method isWindows() returns true
queryCurrentPlatform() sees the true result and sets the currentPlatform variable value (This is not happening as expected!)SystemInfo class initialization is complete, its constructor executes, showing the conflicting values and throwing the exception.Some workarounds stop the problem, but I don't understand why they do:
Performing the Platform.isWindows() check anytime during the instantiation process (including the constructor) properly returns true and assigns the enum appropriately.
currentPlatform variable (removing the final keyword), or ignoring the enum and directly calling JNA's Platform class.Moving the first call to the static method getCurrentPlatform() out of the constructor.
These workarounds imply a possible root cause is associated with executing static methods of multiple classes during class initialization.  Specifically:
Platform.isWindows() check apparently returns false because code reaches the else blockPlatform.isWindows() check returns true.  (Since it is based on a static final value it should not ever return different results.)I've thoroughly reviewed multiple tutorials about Java clearly showing the initialization order, as well as these other SO questions and the linked Java Language Specs:
In Kotlin, we use val to declare an immutable variable and var to declare a mutable variable. You can also optionally specify a type such as String or Int after the variable name. In the example below, we declared a constant firstName of type String with the val keyword.
The only way to initialize static final variables other than the declaration statement is Static block. A static block is a block of code with a static keyword. In general, these are used to initialize the static members. JVM executes static blocks before the main method at the time of class loading.
Declaring variables only as static can lead to change in their values by one or more instances of a class in which it is declared. Declaring them as static final will help you to create a CONSTANT. Only one copy of variable exists which can't be reinitialize.
If a final variable is not initialized during declaration, it must be initialized inside the class's constructor in which it is declared. Such a variable is also called a blank final variable. Any attempt to set a blank final variable outside the constructor will result in a compilation error.
It's not multithreading, because the JVM prevents other threads from accessing a class while it is being initialized. This behavior is mandated by the Java Language Specification, section 12.4.2, step 2:
If the Class object for
Cindicates that initialization is in progress forCby some other thread, then releaseLCand block the current thread until informed that the in-progress initialization has completed, at which time repeat this step.
It is exceedingly unlikely for a JVM to have a bug in this area, since it would cause repeated initializer execution, which would be very noticeable.
However, a static final field can appear to have a changing value if:
there is a cyclic dependency among initializers
Same section, step 3 writes:
If the Class object for
Cindicates that initialization is in progress forCby the current thread, then this must be a recursive request for initialization. ReleaseLCand complete normally.
Therefore, a recursive initialization may allow a thread to read a static final field before it is assigned. This can only happen if class initializers create a cyclic dependency among initializers.
somebody (ab)uses reflection to reassign the static final field
the class is loaded by more than one class loader
In this case, each class has its own copy of the static field, and may initialize it differently.
if the field is a compile time constant expression, and the code was compiled at different times
The spec mandates that compile time constant expressions are inlined by the compiler. If different classes are compiled at different times, the value being inlined may have been different. (In your case, the expression is not compile time constant; I only mention this possiblity for the sake of future visitors).
From the evidence you have given, it is impossible to say which of these apply. That's why I recommend further investigation.
DISCLAIMER: I'm writing this as an answer because I don't know how to make it fit into a comment. If it doesn't help you let me know, and I'll delete it.
Let's start with a small recap which given the question quality, I'm sure you're aware of already:
static to a class, means that it only exists once for any instance. No matter how many instances of the class you create, the field will always point to the same memory address.final means that once initialized, its value cannot change any longer.So, when you mix these two into a static final field, it means:
So, my suspect is not that there is any thread-safety issue (I don't think you're running your tests in parallel, so I guess no two-threads would simultaneously work on these objects, right?), but rather that a previous test of your test suite has initialized the variables differently and, being they run into the same JVM, they don't change any longer their values.
Take this very simple test example.
I have one very basic class:
public final class SomeClass {
    private static final boolean FILE_EXISTS;
    static {
        FILE_EXISTS = new File("test").exists();
    }
    public SomeClass() {
        System.out.println("File exists? " + FILE_EXISTS);
    }
}
The above class simply has a static final boolean saying whether a certain file named test exists into the working directory.
As you can see, the field is initialized once (final) and will be the same for each instance.
Now, let's run these two very simple tests:
@Test
public void test_some_class() throws IOException {
    System.out.println("Running test_some_class");
    File testFile = new File("test");
    if (testFile.exists()) {
        System.out.println("Deleting file: " + testFile.delete());
    } else {
        System.out.println("Could create the file test: " + testFile.createNewFile());
    }
    SomeClass instance1 = new SomeClass();
}
@Test
public void other_test_some_class() {
    System.out.println("Running other_test_some_class");
    SomeClass instance2 = new SomeClass();
}
In the first test, I check if file test exists. If it does exists, I'll delete it. Else, I'll create it.
Then, I'll initialize a new SomeClass().
In the second test, I simply initialize a new SomeClass().
This is the output of my tests run together:
Running other_test_some_class //<-- JUnit picks the second test to start
File exists? false //<-- The constructor of SomeClass() prints the static final variable: file doesn't exist
Running test_some_class //<-- JUnit continues running the first test
Could create the file test: true //<-- it is able to create the file
File exists? false //<-- yet, the initializer of new SomeClass() still prints false
The reason why it prints false, even though we clearly created the test file before initializing new SomeClass(), is that the field FILE_EXISTS is static (hence shared across all instances) and final (hence initialized once, lasting forever).
So if you wonder why private static final int osType; has a value which returns you UNKNOWN when you run mvn install but not when you run the single test, I'd simply look what test, in your full test suite, has already initialized it with a value that you don't expect.
There are two types of solution for this, and they depend on your production code.
It may be that, functionally, you actually need this field to be maybe final to the instance of the class, but not static.
If that's the case, you should just declare it final to the class (once initialized, it doesn't change, but still you have one different value per instance).
Or, it may be that you really need the field to be static final in production, but not during tests as you initialize a new test context each time. If that's the case, you should configure your test plugin to reuseForks = false (meaning a fresh JVM fork is created for each test class, and that guarantees you that each test class will start with a fresh memory for your static final fields):
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>${maven.surefire.plugin.version}</version>
            <configuration>
                <forkCount>1</forkCount>
                <reuseForks>false</reuseForks>
            </configuration>
        </plugin>
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With