How can the value of System.out be changed?
When looking into the implementation of the java.lang.System class, I find
this:
public final static PrintStream out = null;
From that definition alone, I can tell that the value of System.out will
always be null. However, it is initialized when the program starts, and I
can change its value using System.setOut, which performs a security check,
then delegates the call to the setOut0 method which is a native method
that changes the value of System.out. Why is it that I can change the
value of a System.out even though it is declared final?
EDIT: I see that others are voting to mark my question as a duplicate, so
let me explain how it's different: That question is asking why the
nullPrintStream() method is needed (it is a workaround for a bug in older
releases). I'm asking why can the value of a final field can be changed by
native code, and how it can do it.
No comments:
Post a Comment