52

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() ?

tangens
  • 39,095
  • 19
  • 120
  • 139
user69514
  • 26,935
  • 59
  • 154
  • 188

9 Answers9

103

static imports do the trick:

import static java.lang.System.out;

or alternatively import every 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 such a static import of System.out isn't suited for more than simple run-once code."

So please don't imagine that he (or I) think that this solution is Good Practice.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
sfussenegger
  • 35,575
  • 15
  • 95
  • 119
  • 2
    with most ide's these days it will fix it up for you anyway, in eclipse do an organise imports and it will change your java.lang.System.*; to java.lang.System.out; for you (assuming you are only using out) – hhafez Mar 23 '10 at 22:58
31
PrintStream out = System.out;
out.println( "hello" );
tangens
  • 39,095
  • 19
  • 120
  • 139
17

@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 or library more maintainable and more reusable.

(Obviously, if your Java program is a once-off thing that you intend to throw away when you have completed the current task, then maintainability is not a concern. But the flip side is that "throw away" code is often NOT thrown away.)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • 2
    I agree a missing "System." might be confusing at first look. Using local variables or attributes doesn't really change anything though, does it? Using a local `protected void println(Object o) { System.out.println(o);}` might be a good idea though as the output destination could easily be changed, say to `log.info(o)` for instance. – sfussenegger Mar 23 '10 at 23:35
  • 1
    @sfussenegger - use of a local variable or an attribute *does* make it easier to change the destination than using System.out all over the place ... whether System.out is imported or not. – Stephen C Mar 24 '10 at 06:15
  • 1
    okay, that's true. But it's also possible to replace a static import of System.out by a field called out - no need to assign System.out to out. But generally, I agree with you. Using such a static import of System.out isn't suited for more than simple run-once code. – sfussenegger Mar 24 '10 at 07:57
7

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.

Casey
  • 12,070
  • 18
  • 71
  • 107
3

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).

cheesemacfly
  • 11,622
  • 11
  • 53
  • 72
3

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();
Oskar Kjellin
  • 21,280
  • 10
  • 54
  • 93
  • and now you don't "create" an object :) – sfussenegger Mar 23 '10 at 22:48
  • The official java documentation uses the term "Create object" aswell as the one i guess you are referring to "instantiate" http://java.sun.com/docs/books/tutorial/java/javaOO/objectcreation.html – Oskar Kjellin Mar 23 '10 at 22:51
  • You don't create or instantiate an object at all. You simply copy the reference. Hence `System.out` and `out` will reference the same object, i.e. `System.out == out` will be `true` – sfussenegger Mar 23 '10 at 22:57
  • oh, missed your "now" part :P By the way I haven't programmed java that much as you might notice – Oskar Kjellin Mar 23 '10 at 23:10
1

you can see this also in sockets ...

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

out.println("hello");
Exorcismus
  • 2,243
  • 1
  • 35
  • 68
1

simply import :

import static java.lang.System.*;
Tharif
  • 13,794
  • 9
  • 55
  • 77
0

Or simply:

System.out.println("Some text");
Chris Knight
  • 24,333
  • 24
  • 88
  • 134