[java] Java out.println() how is this possible?

I've seen some code such as:

out.println("print something");

I tried import java.lang.System;

but it's not working. How do you use out.println() ?

This question is related to java printing libraries

The answer is


PrintStream out = System.out;
out.println( "hello" );

You will have to create an object out first. More about this here:

    // write to stdout
    out = System.out;
    out.println("Test 1");
    out.close();

Well, you would typically use

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

which doesn't require any imports. However, since out is a static field inside of System, you could write use a static import like this:

import static java.lang.System.*;

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

Take a look at this link. Typically you would only do this if you are using a lot of static methods from a particular class, like I use it all the time for junit asserts, and easymock.


out is a PrintStream type of static variable(object) of System class and println() is 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 is accessed with the class name System which further invokes the method println() of it's type PrintStream (which is a class).


you can see this also in sockets ...

PrintWriter out = new PrintWriter(socket.getOutputStream());

out.println("hello");

simply import :

import static java.lang.System.*;

Or simply:

System.out.println("Some text");

@sfussenegger's answer explains how to make this work. But I'd say don't do it!

Experienced Java programmers use, and expect to see

        System.out.println(...);

and not

        out.println(...);

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

  • it breaks the accepted idiom, and
  • it makes it harder to track down unwanted trace prints that were added during testing and not removed.

If you find yourself doing lots of output to System.out or System.err, I think it is a better to abstract the streams into attributes, local variables or methods. This will make your application more reusable.


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to printing

How do I print colored output with Python 3? Print a div content using Jquery Python 3 print without parenthesis How to find integer array size in java Differences Between vbLf, vbCrLf & vbCr Constants Printing variables in Python 3.4 Show DataFrame as table in iPython Notebook Removing display of row names from data frame Javascript window.print() in chrome, closing new window or tab instead of cancelling print leaves javascript blocked in parent window Print a div using javascript in angularJS single page application

Examples related to libraries

Gradle failed to resolve library in Android Studio Add external libraries to CMakeList.txt c++ The type java.util.Map$Entry cannot be resolved. It is indirectly referenced from required .class files fatal error LNK1104: cannot open file 'kernel32.lib' Node.js Logging The project was not built since its build path is incomplete libstdc++-6.dll not found How to add local jar files to a Maven project? How to add additional libraries to Visual Studio project? Java out.println() how is this possible?