koushik koushik - 3 months ago 8x
Java Question

Why cant we use "PrintStream.out.println()" instead of "System.out.println()", as 'out' belongs to 'PrintStream' class?

I am a Beginner to programming. I have this unusual doubt:

Tell me the difference between

, please.

  1. System is a class that has a few static fields to provide your Java program access to very basic operating system facilities.

  2. OutputStream is a class that provides methods to write binary data into a stream (such as a file or network connection).

  3. PrintStream is a subclass of OutputStream that provides methods to print text and other types of variables (like boolean or int) to an underlying OutputStream. It converts Strings to binary for example (so that it can be written out).

  4. System.out is an instance of PrintStream that is connected to the standard output stream.

  5. The field out in PrintStream is (unlike System.out) not a static field. This means that you need an instance of PrintStream to access it. myPrintStream.out rather than PrintStream.out.

  6. The field out in PrintStream instances is the underlying OutputStream that is being wrapped.

    This is intended for implementations of PrintStream to write their binary output to.

    It is also protected, so that it is only accessible to those implementations.

    You cannot access System.out.out (or myPrintStream.out) directly (but you don't need to, as System.out will forward everything you write)