3

Here:

System.out.println("Hi, this is frist program");

Is println() a static member function of PrintStream class or instance member function?

As told by my teacher : that when there is dot (.) after class name then definitely we are trying to access the static member of the class.

As here out is static reference variable and it has reference of PrintStream class. So my question is that is the println() function has necessarily to be a static member function?

Tunaki
  • 132,869
  • 46
  • 340
  • 423
Infinity
  • 125
  • 2
  • 9

4 Answers4

5

No, println is an instance method of the PrintStream class. It is out that is a static member of the System class.

System.out
      ^--^
        static member of the class System, returns a PrintStream instance

System.out.println(...)
          ^------^
            instance method of PrintStream

out is declared as

public static final PrintStream out

in the class System, so it is a static member and you access it with System.out (refer to this question for the final modifier).

println() is an instance method of the PrintStream class declared as

public void println()
Community
  • 1
  • 1
Tunaki
  • 132,869
  • 46
  • 340
  • 423
1

No, println is an instance method of PrintStream, as you can see in the Javadoc of PrintStream.

void java.io.PrintStream.println(String x)

System.out gives you a reference to a PrintStream instance for which println is executed.

Eran
  • 387,369
  • 54
  • 702
  • 768
1

println() is a non static (instance) member function of PrintStream class.

out is a static reference to an object of type PrintStream, inside System class.

out is a reference, out.prinln() refers to the instance method of out. In this case out is also a static member of another class, but this doesn't make any difference to the println() method

Luigi Cortese
  • 10,841
  • 6
  • 37
  • 48
0

It's an instance method.

System.out is a static reference to a PrintStream instance. https://docs.oracle.com/javase/7/docs/api/java/lang/System.html#out and this provides multiple overloaded versions if println https://docs.oracle.com/javase/7/docs/api/java/io/PrintStream.html#println(java.lang.String)

Pascal
  • 8,464
  • 1
  • 20
  • 31