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

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

Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top