Strictly, the answer to your question "How can I override System.out.println()?" is that you can replace System.out
with an alternative PrintStream
using System.setOut
. The PrintStream
you supply can implement println
however you like (*).
But this is almost certainly not what you mean. You can override the toString()
method on your class instead, and then this will be used in all places that want to build a string representation of your instance, not simply System.out.println
.
(*) However if you want to conform to the specification, you don't have that much flexibility, e.g. println(Object)
"calls String.valueOf(x)
".