Java out.println() how is this possible?

前端 未结 9 1444
难免孤独
难免孤独 2020-12-08 13:42

I\'ve seen some code such as:

out.println(\"print something\");

I tried import java.lang.System;

but it\'s not working

9条回答
  •  醉酒成梦
    2020-12-08 14:04

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

提交回复
热议问题