Java out.println () how is this possible?

I saw some code, for example:

out.println("print something"); 

I tried import java.lang.System;

but it does not work. How do you use out.println() ?

+41
java libraries printing
Mar 23
source share
9 answers

static import does the trick:

 import static java.lang.System.out; 

or, conversely, import each static method and field using

 import static java.lang.System.*; 



Addendum by @Steve C: note that @sfussenegger said this in a comment on my answer.

"Using this static import System.out is not suitable for simpler code with a single run."

Therefore, please do not think that he (or I) think that this decision is good practice.

+79
Mar 23 '10 at 22:37
source share
 PrintStream out = System.out; out.println( "hello" ); 
+26
Mar 23
source share
Answer

@sfussenegger explains how to do this. But I would say do not do this!

Experienced Java programmers use and expect to see

  System.out.println(...); 

but not

  out.println(...); 

Static import of System.out or System.err is (IMO) bad style because:

  • he violates the accepted idiom and
  • this makes it difficult to track unwanted trace fingerprints that were added during testing and not deleted.

If you find that you are doing a lot of results in System.out or System.err, I think it's best to abstract the threads in attributes, local variables, or methods. This will make your application more reusable.

+14
Mar 23 '10 at 23:26
source share

Ok, you usually use

 System.out.println("print something"); 

which does not require import. However, since out is a static field inside the System, you can write the use of static imports as follows:

 import static java.lang.System.*; class Test { public static void main(String[] args) { out.println("print something"); } } 

Look at the link. Usually you only do this if you use a lot of static methods from a particular class, for example, I use it all the time for junit and easymock statements.

+5
Mar 23 '10 at 22:38
source share

out is a PrintStream type of static variable (object) of the System class and println() is a function of the PrintStream class.

 class PrintStream { public void println(){} //member function ... } class System { public static final PrintStream out; //data member ... } 

That is why the static variable (object) out accessed by the name of the System class, which additionally calls the println() method of this type of PrintStream (which is a class).

+3
Jan 30 '13 at 17:24
source share

First you need to create an out object. More on this here:

  // write to stdout out = System.out; out.println("Test 1"); out.close(); 
+1
Mar 23 '10 at 22:35
source share

you can see it also on sockets ...

 PrintWriter out = new PrintWriter(socket.getOutputStream()); out.println("hello"); 
+1
Aug 27 2018-12-12T00:
source share

just import:

 import static java.lang.System.*; 
+1
Oct 13 '13 at 23:35
source share

Or simply:

 System.out.println("Some text"); 
0
Mar 23 '10 at 22:38
source share



All Articles