nohup
nohup

Reputation: 3165

Why can't I use the print() or println() method in java.io.PrintStream as it is after importing the class?

Apologies for this silly question, but while I was learning java classes, I tried the following

javap -c java.lang.System | grep -i out
  public static final java.io.PrintStream out;

javap java.io.PrintStream | grep print
public void print(boolean);
public void print(char);
public void print(int);
public void print(long);
public void print(float);
public void print(double);
public void print(char[]);
public void print(java.lang.String);
public void print(java.lang.Object);
public void println();
public void println(boolean);
public void println(char);
public void println(int);
public void println(long);
public void println(float);
public void println(double);
public void println(char[]);
public void println(java.lang.String);
public void println(java.lang.Object);
public java.io.PrintStream printf(java.lang.String, java.lang.Object...);
public java.io.PrintStream printf(java.util.Locale, java.lang.String, java.lang.Object...);

And I tried to see if I can import java.io.PrintStream and use print() or println() as it is, instead of System.out.println().

import java.io.PrintStream;
println('a');

And it came out with a compile error saying

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    The method print(char) is undefined for the type array
    at array.main(array.java:16)

Why can't I use println() as it is after importing java.io.Printstream ?

Upvotes: 5

Views: 28876

Answers (4)

Arun Raaj
Arun Raaj

Reputation: 1800

You will have to instantiate PrintStream class but it doesn't have a default no-arg constructor.
So it's an easy way to use its static instance from System class and call the print() method directly.

Upvotes: 0

David Kühner
David Kühner

Reputation: 803

You need an instance of PrintStream because println is not static.

You can try this:

import java.io.PrintStream;
PrintStream printStream = new PrintStream(System.out);
// or better
PrintStream printStream = System.out;
printStream.println('a');

PrintStream needs a OutputStream for the constructor, you can give the OutputStream you want:

ByteArrayOutputStream, FileOutputStream, FilterOutputStream, ObjectOutputStream, OutputStream, PipedOutputStream

Javadoc : OutputStream PrintStream

Upvotes: 1

dany-freezee
dany-freezee

Reputation: 11

In Java You always need to call a method (function) on a specific object. That's why if you want to call any of these methods (print, println) you need to create the object of type java.io.PrintStream first.

For example, try the following code:

import java.io.PrintStream;
...
PrintStream ps = System.out;
ps.print('a');

It creates the PrintStream object which prints to the cosole and prints the given char argument there.

Upvotes: 0

Grodriguez
Grodriguez

Reputation: 21985

Because println is an instance method of the PrintStream class, and you need an instance of a class to call instance methods.

However, System.out is an instance of PrintStream, so you can do:

 System.out.println("blah blah")

or you can create a new PrintStream instance, for example to write to a file:

 PrintStream p = new PrintStream(filename);
 p.println("blah blah");

This section in the Java Tutorial can be helpful: Lesson: Classes and Objects

Upvotes: 10

Related Questions