+ 1

Why System.out when printed gives it's address and not null ?

System.out is initialized in the System class like this : public final static PrintStream out = null; Then why when I'm printing it, it's address is coming as the output and not null. i.e, System.out.println(System.out); Output : java.io.PrintStream@18025c The value after @ is changing each time. As out is declared final so it should not change and print null but it is not doing so, why? And about finding the above line, let me say that Java ships with this line of code, I found it in System class.

15th Feb 2018, 5:33 PM
SDJava
SDJava - avatar
2 odpowiedzi
+ 1
That's its initial value. It's no longer null because you're passing a value to output. Your information was vague, so if I misunderstood, please provide more information for clarity.
15th Feb 2018, 5:55 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 1
public final static PrintStream out = null; Where did you find this code? if out object is null, then all the calls to System.out.print() should throw NullPointerException, however that is not true. That means this object is being initialized somewhere. I got your question. Thought it is initially assigned NULL, it can be changed using Java reflection API (might be little intimidating for the beginners). You can take a look the following SO answer for more details. https://stackoverflow.com/questions/31743760/system-out-is-declared-as-static-final-and-initialized-with-null
15th Feb 2018, 6:01 PM
Ravi Chandra Enaganti
Ravi Chandra Enaganti - avatar