2

In System.java, out is instantiated by:

public final static PrintStream out = nullPrintStream();

You would expect this to return a valid PrintStream object, except this is what the method looks like:

private static PrintStream nullPrintStream() throws NullPointerException {
    if (currentTimeMillis() > 0) {
        return null;
    }
    throw new NullPointerException();
}

This will always return null. However, calling something like System.out.println() calls the println() method on a valid PrintStream object instead of throwing a null pointer exception. Can anyone explain why this works and how/where the PrintStream object is actually being instantiated?

Itchydon
  • 2,572
  • 6
  • 19
  • 33
Dan
  • 474
  • 1
  • 7
  • 16

0 Answers0