I\'ve seen some code such as:
out.println(\"print something\");
I tried import java.lang.System;
but it\'s not working
Or simply:
System.out.println("Some text");
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).
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.