直接设置System.out,
System.out = null; //gives error that it is final and that's understood但是看看这个:
System.setOut(null); //allows to set out ??但是如果out是final,那怎么可能呢?
发布于 2012-12-04 21:17:15
简单的答案是,使用本机C编写的东西,特别是库类,不一定要遵循与您和我编写纯Java相同的规则。
编辑:,在进一步研究之后,我们甚至可以更改final字段(前提是安全管理不反对)。请参阅https://stackoverflow.com/a/3301720/367273
发布于 2012-12-04 21:11:43
这是在很低的水平上完成的。在Java级别上,您甚至不能将null分配给final变量。JDK 1.7的源代码:
/**
* 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
}发布于 2012-12-04 21:12:34
在第一个实例中,静态变量System.out是最终变量,因此不能设置它。
在第二个实例中,您正在调用一个静态方法,该方法将标准输出设置为不同的流。
https://stackoverflow.com/questions/13711979
复制相似问题