开发者

System.out.println() is a method to a field?

开发者 https://www.devze.com 2022-12-15 18:43 出处:网络
if out is a field to the System class. how is it that you can use println() method in a field? and where does this println()开发者_运维知识库 method come from, i cant find it in the System class.Syste

if out is a field to the System class. how is it that you can use println() method in a field? and where does this println()开发者_运维知识库 method come from, i cant find it in the System class.


System.out is a public instance of the PrintStream class:

https://docs.oracle.com/javase/1.5.0/docs/api/java/io/PrintStream.html


since System.out is a (reference to an instance of) PrintStream,
we can write System.out.println() as:

    PrintStream output = System.out;
    output.println();


in, out, and err are all public static fields of java.lang.System. There's nothing preventing you from marking a field as public, although generally it is considered bad form (use getters and setters instead).

System.out is of type PrintStream, which provides the println method.

See as a starting point: http://java.sun.com/j2se/1.5.0/docs/api/java/lang/System.html#out


System.out is of type PrintStream, which has a println() method.

You can see all the methods of PrintStream at http://java.sun.com/j2se/1.4.2/docs/api/java/io/PrintStream.html.

And here are the fields and methods of System: http://java.sun.com/j2se/1.5.0/docs/api/java/lang/System.html

More generally, a field is just a member of an Object, so it can be of any type. If it's a public field then it is accessible to the client code, though generally it's considered better practice to make fields accessible through getter methods as opposed to directly (but in this case, imagine if you had to write System.getOut().println() every time you wanted to write to standard output!).


System.out is a member of System, alright, but is is also a reference to a PrintStream instance. From the source:

public final static PrintStream out = nullPrintStream();

PrintStream is a class in java.io, and has the method println(). See the JavaDocs for java.io.PrintStream.println() for more details.


If a field is public and is an Object, there is nothing surprising in being able to call a method on it. Why would it be?

In the particular case of the System class, the out field is a public static field of type PrintStream (it holds an instance of a PrintStream statically which has a println() method) and this is what the javadoc says about it:

The "standard" output stream. This stream is already open and ready to accept output data. Typically this stream corresponds to display output or another output destination specified by the host environment or user.

For simple stand-alone Java applications, a typical way to write a line of output data is:

     System.out.println(data)

See the println methods in class PrintStream.


System is a class as you know and out is a static variable of class System of type PrintStream i.e inside System class, out is declared as :

public static final PrintStream out;

That also means out is an instance of class PrintStream so it can access methods defined in class PrintStream.

So as we are accessing static variable out outside class System we write

System.out  

and to access methods like print or println of class PrintStream we write

System.out.print()

as System.out (or out) is nothing but a variable pointing to an object of class PrintStream as said above.

We could also write

Printstream john;  
john = new Printstream();
john = system.out;  //now both variable point to same object
john.println();     //instead of system.out.println()


System.out is a static instance of a PrintStream object.


In general, when you have a field of some non primitive data type, you have to inspect the class of the field, not the class that owns the field, to check for available methods.

So in this case even if the class System has a static field out, the type of that field is PrintStream, not System, which is in turn the class that owns out.

0

精彩评论

暂无评论...
验证码 换一张
取 消