1

Please explain out variable in System.out

out is sometimes referred to be object of type OutputStream sometimes referred to as object of type PrintStream

and even when it is predefined variable, it is sometimes assigned it to PrintWriter object

PrintWriter out= response.getWriter();

is it due to the fact that a superclass reference can be assigned the reference to objects of its subclass?

3 Answers 3

2

Super class reference variable can hold the reference of sub-class object. The OutputStream is an abstract super class of all OutputStream of bytes classes, so you can say the System.out field as OutputStream type.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank You Sir. Have a lovely day!
1

PrintStream is a subclass of OutputStream, and System.out is a PrintStream, so it truly is both.

The line:

PrintWriter out= response.getWriter();

has nothing to do with System.out. I don't know where that line of code is from. It's defining a local variable named out that's totally independent of System.

4 Comments

The PrintWriter line is likely from a servlet to enable writing directly to the response, and yep, a totally separate thing from System.out.
response is probably a javax.servlet.ServletResponse
Please forgive me for my negligence respected Programmers. PrintWriter out = response.getWriter is from a servlet code. Since System.out is usually used in programs, so i thought that "out" must be a reserved word and hence it should not be used any other way.
Thank You very very much Ed Sir, Dave Sir and Stephen Sir! Have a lovely day. Thank You!
1

To the best of my knowledge, in System.out, "out" is a name of a method. When you say System.out.print() you call the System class and its out() method. This out() is a static method which gives you the reference to "System" class object. So after you get the reference you call the print() method.

But in, PrintWriter out= response.getWriter(); you are just creating a reference variable of PrintWriter class. So as my fellow colleagues mention, there is no connection between "System.out" and "PrintWriter out". There are two purposes for those two.

Correct me if I'm wrong. Thanks.

1 Comment

Thank You Sir. But there in System class, it is perhaps a variable. Please check it. Have a lovely day!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.