-3

can i build a user-defined class for printing anything using printstream variable just as "System" class ?

so what i did is:

    import java.io.*;

    public final class SystemDemo{



public final static InputStream give=null;//the same variables 

public final static PrintStream take=null;

public final static PrintStream error = null;

}

and wanted to use this SystemDemo class as "System"(pre-defined) ,like

    SystemDemo.take.println("something");

but i get a NullPointerException .

nikhil2000
  • 178
  • 3
  • 15

1 Answers1

0

You can try this, will work same for your requirement,

 public final class SystemDemo
 {
    public final static PrintStream take =  new PrintStream(System.out);
 }

And then use this as,

SystemDemo.take.println("Your String");

It worked fine, tested with Netbeans.

  • thanks @mrunmay-deswandikar – nikhil2000 Feb 10 '18 at 08:37
  • Yea. This is the part that you can do safely. The part that you can't do is emulate the behavior of `System.setIn()` etcetera and the "magic" that `System` uses to *actually* initialize the `final` variables. Using nasty reflection or `Unsafe` is not guaranteed to work reliably for modifying `final` variables. – Stephen C Feb 11 '18 at 02:03
  • Right, the main part is we can't get from API, how the out object is internally initialized, if we try to initialize, any custom Out object, we can't manage it. Java internally handles this. – Mrunmay Deswandikar Feb 19 '18 at 12:16