Results 1 to 4 of 4
  1. #1
    esolve is offline Member
    Join Date
    Mar 2015
    Posts
    56
    Rep Power
    0

    Default System.out used as PrintStream constructor arguments, what will happen?

    Java Code:
    public class NullPrintStream extends PrintStream
    {
    
        public NullPrintStream()
        {
    	super(System.out);
        }
    
    ...
    }

    does the above class redirect the buffered streams in System.out to itself, so that when the program output something to System.out, the content is redirected to NullPrintStream.

    For example,

    Java Code:
            System.out.println("Test content");
    then I won't see "Test content" on console, coz it is redirected to NullPrintStream, am I right?

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: System.out used as PrintStream constructor arguments, what will happen?

    PrintStream has a constructor that takes an OutputStream and wraps it to add additional functionality (see the Java docs).
    In this case that code is using System.out as the base stream to use.
    It's not changing that stream.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    esolve is offline Member
    Join Date
    Mar 2015
    Posts
    56
    Rep Power
    0

    Default Re: System.out used as PrintStream constructor arguments, what will happen?

    so you mean System.out.println("Test content"); can still output "Test content" to console?

    then does the source codes use System.out as the base of NullPrintStream? are there any benefits?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: System.out used as PrintStream constructor arguments, what will happen?

    The System class has methods setIn( ... ), setOut( ... ) and setErr( ... ) that set a new input, output or error stream. If you don't use those methods, the original streams will be used by the System class, no matter if you wrapped them in other streams.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Replies: 10
    Last Post: 09-03-2012, 08:13 PM
  2. Why must constructor arguments be stored manually?
    By Daimoth in forum New To Java
    Replies: 9
    Last Post: 05-29-2012, 08:51 PM
  3. Replies: 3
    Last Post: 01-23-2012, 11:13 AM
  4. Creating a constructor with arrays and arguments - Part 2
    By fullmetaljacket in forum New To Java
    Replies: 15
    Last Post: 07-05-2009, 01:38 PM
  5. Creating a constructor with arrays and arguments
    By fullmetaljacket in forum New To Java
    Replies: 38
    Last Post: 07-03-2009, 06:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •