Why is Java's "out/in" PrintStream field designed to be referenced directly rather than using a getter

StackOverflow https://stackoverflow.com/questions/18676952

This is something I've always been curious about since my first "Hello World"

Why is Java's out PrintStream a static field that is designed to be used rather than making something like a "getOut()" method that would return the PrintStream. Everything I've learned about good coding practices screams that this is the best way to do things. Why doesn't Java do it?

有帮助吗?

解决方案

The immediate reason is that the System streams predate the JavaBeans model with its get/set/is nomenclature, which was added in 1.1; the System streams go all the way back to 1.0, before even inner classes, so backwards compatibility demands continuity.

A related reason is that calls to getters can't be as efficiently inlined as direct field references, even if the methods are final, and since printing output is so common, it's likely that even today, those fields would be used directly.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top