In the System class, in, out, and err are static fields. These fields are declared for example:
public final static InputStream in = nullInputStream();
Why declare nullInputStream() instead of null?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The source code has the following comment:
In short, since
System.inis astatic finalvariable, if it was set tonull, the compiler would consider it as a constant, and would replace all the references toSystem.inin other classes withnull(that’s what inlining means). Which would obviously make everything non-functional. Some native code must be used to replace the value of thisSystem.infinal value (which normally should never change) once the system is initialized.To resume: it’s used to avoid a compiler optimization that should not be made in this very particular case, because System.in is a final field that can change, which is normally impossible.