-1

I'm trying to understand how System.out can invoke println(). All the searches say System.out is an instance of PrintStream class therefore it can invoke a method in that class.

BUT...

When I try to duplicate this with the following a NullPointerException results.

public class Main {
    public static void main(String[] args){
        Tasker.calc.add(1, 2);
    }
}

class Tasker{
    static Calc calc;
}

class Calc {
    public void add(int x, int y){
        System.out.println(x+y);
    }
}

Isn't Tasker.calc a similar instance of Calc? How is System.out not a NullPointerException?

To be clear, this is not a question about NullPointerException... this is a question about why System.out doesn't require initialization.

Thanks a bunch!

  • You never intialized `Tasker.calc`, hence you are getting this exception. You might want to read a good Java tutorial before you get started, Oracle's trails are a good start. – Tim Biegeleisen Apr 19 '17 at 05:08
  • You have not initialized calc – Courage Apr 19 '17 at 05:08
  • You have not initialized the `calc` variable. Therefore it has the value `null`. (By contrast, the Java runtime automatically initializes `System.out` to a non-null value.) – Stephen C Apr 19 '17 at 05:08
  • I am thinking what to dup-close this too... What is Nullpointerexception? Thoughts anybody? – GhostCat Apr 19 '17 at 05:10
  • How come I don't have to initialize System.out with 'new'? – J.McLachlan Apr 19 '17 at 05:10
  • 1
    Because the system takes care of that. It is special. (And besides, there is no way that you *could* correctly initialize it. That variable is set to point to the JVM's standard output stream, and there is no way to find out what it should be ...) Hint: *read the javadocs!* – Stephen C Apr 19 '17 at 05:12
  • [What does your step debugger tell you?](http://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) –  Apr 19 '17 at 05:13
  • Read up what `static` variables are and how they work. –  Apr 19 '17 at 05:14
  • @JarrodRoberson - this is nothing to do with static methods. It is about the (default) initialization of fields ... and the unusual behavior of `System.xxx`. – Stephen C Apr 19 '17 at 05:15
  • @TimBiegeleisen et al. This is not really about NPEs either. – Stephen C Apr 19 '17 at 05:16
  • Thank you David! That hit the spot :) – J.McLachlan Apr 19 '17 at 05:17
  • @StephenC Agree with your Javadoc comment, disagree about closing as a dupe, but since you outrank me I won't touch this question again. – Tim Biegeleisen Apr 19 '17 at 05:19
  • `System.out` is a `static reference to an outputstream` so it has everything to do with `static` variables. –  Apr 19 '17 at 13:57

3 Answers3

2

Why doesn't System.out require initialisation? Well ...

Deep inside the System class, there's a method called initializeSystemClass which the JVM calls. It sets System.out to the right value, so you don't have to.

Note that System.out is NOT an instance of PrintStream - it's a REFERENCE to an instance of PrintStream.

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
1

You haven't _initialized calc. You could have written

static Calc calc = new Calc();

and it would have worked (depending on the definition of Calc).

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
0

As @LouisWasserman points out, the reason that your code gives an NPE is that you haven't initialized the calc variable. As a result, it gets default initialized to null. The solution: initialize it!

But the deeper question is why System.out doesn't need to be initialized. That is because the Java runtime initializes it behind the scenes to point to a PrintStream that refers to your application's (external) standard output.

  • You don't need to initialize System.out because the system does it. The javadocs say so.
  • You couldn't initialize it in the nromal way because System.out is declared as final. (But there is System.setOut(...))
  • Even with System.setOut(...) there is no way for an application to find out what System.out should be set to. (Consider this java Test | wc )
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216