Since final can be initialized only once then how these are getting managed ?
While you can change static final
variable via reflection, in this case the fields are changed via native methods.
From java.lang.System
public static void setIn(InputStream in) {
checkIO();
setIn0(in);
}
// same for setOut(), setErr()
private static native void setIn0(InputStream in);
private static native void setOut0(PrintStream out);
private static native void setErr0(PrintStream err);
When we use System.out.print("..."); It is obvious that out is not null but being a final static how it is not null ?
It gets set before you have a chance to use it.
You might wonder why it does this? The answer is almost certainly related to the order classes are loaded. Many of the classes startup in order but they need to be initialised in an order which works.
So can any one explain that how out is initialized which is already declared final ?
This is because final
isn't as final as you might think. It has been suggested we need a final final
.