views:

332

answers:

6

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

+6  A: 
PrintStream out = System.out;
out.println( "hello" );
tangens
+1: Works nicely and simple, too.
S.Lott
+1 Hah, never thought of that ^^.
Helper Method
+1 for the principle. However, isn't it `PrintStream` that exposes the `println` method, not `OutputStream`?
Adam Paynter
@adam-paynter: Thanks, I corrected that.
tangens
+1  A: 

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
that's one weird class
sfussenegger
Fixed it.......
Oskar Kjellin
and now you don't "create" an object :)
sfussenegger
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
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
oh, missed your "now" part :P By the way I haven't programmed java that much as you might notice
Oskar Kjellin
+12  A: 

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.*;
sfussenegger
An import * is not necessary. For the case presented by the OP `import static java.lang.System.out;` would be enough. Anyway +1.
smink
@smink you're absolutely right but I'm a pretty lazy typer ;) I've changed it anyway
sfussenegger
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
isn't it interesting the the easiest questions yield the most reputation. Should be the other way arround!
Oskar Kjellin
@Kurresmack that's what you get for good karma gained by answering tons of bad-ass questions without getting more than 25 reputaion ;)
sfussenegger
Haha, must be something like that :P
Oskar Kjellin
+1  A: 

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
`out` isn't a method, it's a field.
sfussenegger
doh! Thanks for catching that. I meant to say field.
Casey
A: 

Or simply:

System.out.println("Some text");
Chris Knight
+1  A: 

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

Stephen C
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
@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
@Stephen - 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