System.out.println I have a question.

Asked 1 years ago, Updated 1 years ago, 126 views

Opening System.out.println(). I understand that you can access the object out declared as PrintStream in the object called System and access println, which is the method of PrintStream.

However, when System declares out, it declares public static final PrintStream out = null; and ends.

So out means that an object is not created, but if you can access println (and println is not a static method), it means that an object is created, so can you tell me where it is?

Or am I mistaken?

java output

2022-09-21 23:11

1 Answers

Hello, everyone I looked it up and found out that Out is the Instance of PrintStream.

If you look at java.io.PrintStream, the constructor touches out through this() or super(). But the out structure comes from OutputStream.

java.lang.Object
 java.io.OutputStream
      java.io.FilterOutputStream
            java.io.PrintStream

When I opened the FilterOutputStream,

public class FilterOutputStream extends OutputStream {

    protectedOutputStreamout; //Here

    private volatile boolean closed;

    private final Object closeLock = new Object();

    public FilterOutputStream(OutputStream out) { 
          This.out = out; //Here
    }

When I opened PrintStream

public class PrintStream extends FilterOutputStream implements Appendable, Closeable
{

  67     private final boolean autoFlush;
  68     private boolean trouble = false;
  69     private Formatter formatter;

  75     private BufferedWriter textOut;
  76     private OutputStreamWriter charOut;

 107   private PrintStream(boolean autoFlush, OutputStream out) {
 108 super(out); //here
 109         this.autoFlush = autoFlush;
 110         this.charOut = new OutputStreamWriter(this);
 111         this.textOut = new BufferedWriter(charOut);
 112     }
 113

 119     private PrintStream(boolean autoFlush, Charset charset, OutputStream out) {
 120 this(out, autoFlush, charset); //Here
 121     }

Note: https://docs.oracle.com/javase/7/docs/api/java/io/PrintStream.html


2022-09-21 23:11

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.