I think your second image is showing a snippet of the code of java.lang.System
.
And I think you are asking "how come System.out.println
works despite System.out
being final
and initialized to null
".
The answer is ... magic!!
There is some deep dark magic in the JVM initialization that reassigns non-null values to System.in
, System.out
and System.err
. Indeed, the specialness of these fields even gets a mention in the Java Language Specification (17.5.4 if you are curious).
Similar magic is used to make System.setOut(...)
and friends work.
IMO, it is best to just trust that it works ... and not ask how.
And the corollary is that the reason your attempt to do something similar doesn't work is that your x
field is behaving as expected. It has been initialized to null
, and it stays that way. No magic going on here. Your code isn't "special".
(For what it is worth, in some contexts it is possible to use reflection to modify the value of a final
field. But it is a bad idea. For a start, the JLS carefully does not specify what happens when you do this; see 17.5.3.)