System.out
、の直接設定
System.out = null; //gives error that it is final and that's understood
しかし、これを見てください:
System.setOut(null); //allows to set out ??
しかし、そうである場合、それはどのように可能ですout
かfinal
?
System.out
、の直接設定
System.out = null; //gives error that it is final and that's understood
しかし、これを見てください:
System.setOut(null); //allows to set out ??
しかし、そうである場合、それはどのように可能ですout
かfinal
?
非常に低いレベルで行われます。レベルでは、変数にさえ割り当てるJava
ことはできません。JDK 1.7 のソース コード:null
final
/**
* Reassigns the "standard" output stream.
*
* <p>First, if there is a security manager, its <code>checkPermission</code>
* method is called with a <code>RuntimePermission("setIO")</code> permission
* to see if it's ok to reassign the "standard" output stream.
*
* ...
*/
public static void setOut(PrintStream out) {
checkIO();
setOut0(out); //native method
}
簡単な答えは、ネイティブCを使用して記述されたもの、特にライブラリクラスは、必ずしもあなたや私が純粋なJavaを記述しているのと同じルールで実行する必要はないということです。
編集:さらに調査すると、あなたと私でさえfinal
フィールドを変更できることがわかりました(管理されているセキュリティが反対しない場合)。https://stackoverflow.com/a/3301720/367273を参照してください
最初のインスタンスでは、静的変数 System.out は final であるため、設定できません。
2 番目のインスタンスでは、標準出力を別のストリームに設定する静的メソッドを呼び出しています。