1

Variables modified as final must be initialized when declared or when the constructor is executed.

I looked up in the System Class File, and found the out Object is initialized in the private static void initializeSystemClass() method, when is this method called?

Amal K
  • 4,359
  • 2
  • 22
  • 44
Clarence
  • 19
  • 5
  • 1
    Did you search the same source file to see where that method was called? – kaya3 Jun 07 '21 at 08:55
  • @kaya3 Did you? :) It's not invoked from within that class file. Regardless, the reason for needing this information would be useful to know because in any common use case the answer to the actual question is irrelevant. The bottom line is that it is not specified so there is no definitive answer. Whatever your JRE implementation does with it can not be reliably transferred to any other JRE. – Torben Jun 07 '21 at 08:59
  • @Torben I did not, because the OP did not link to the source file and I am not going out of my way to find it. It also plausibly could depend on which version/implementation of Java they are asking about, but the question has no details about that either. – kaya3 Jun 07 '21 at 09:04
  • It's worth noting that `System.setOut` will change the `final` `out` field. This field,`in` and `err` are treated specially by the *Java Memory Model*. – Tom Hawtin - tackline Jun 10 '21 at 16:42

2 Answers2

1

This method is called by JVM as per the comments in System class. Added portion of comments below:

public final class System {

    /* register the natives via the static initializer.
     *
     * VM will invoke the initializeSystemClass method to complete
     * the initialization for this class separated from clinit.
     * Note that to use properties set by the VM, see the constraints
     * described in the initializeSystemClass method.
     */
    private static native void registerNatives();
    static {
        registerNatives();
    }

    /** Don't let anyone instantiate this class */
    private System() {
    }
pcsutar
  • 1,715
  • 2
  • 9
  • 14
0
private static native void registerNatives();
static { registerNatives();
}

in the System.java file, we can see this code in the head line. it invokes the registerNatives Method, it will let the VM to call the initializeSystemClass Method to initialize the System Class.

Clarence
  • 19
  • 5