Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the System.out object belong to class System or class PrintStream? [closed]

I'm new to programming and I just started learning Java. I'm curious that does the object System.out belong to class System or class PrintStream?

I referred to a textbook, JAVA CONCEPTS 4/e. The textbook states that to use the out object in the System class, you must refer it as System.out but later in the book it states that the System.out belongs to class PrintStream.

I've searched google and stackoverflow but all the answers are too hard for me to understand.

like image 227
user2236096 Avatar asked May 16 '13 14:05

user2236096


People also ask

Is System out an object or a class?

The out is an instance of the System class and is of type PrintStream. Its access specifiers are public and final. It is an instance of java. io.

What type of stream is System out?

Out is a static field inside System class. The type of out is PrintStream .

Can we create object for PrintStream class in Java?

Create a PrintStreamOnce we import the package here is how we can create the print stream. // Creates a FileOutputStream FileOutputStream file = new FileOutputStream(String file); // Creates a PrintStream PrintStream output = new PrintStream(file, autoFlush);

Why we Cannot create the object to PrintStream class directly?

Can't we just create an object of PrintStream class and call the println() method as PrintStream class can be instantiated. In that case the default implementation is gone. You have to, say for example, create a file, then pass it... You can try some code to check what all you can do!


4 Answers

"out" belongs to class "System" and is of type "PrintStream" :)

like image 65
Philip Helger Avatar answered Sep 28 '22 07:09

Philip Helger


It depends what you mean by "belongs to".

Usually, people would say out "belongs" to System because it is a static field declared in that class. Note, though, that this concept of belonging is only a weak one, basically implying only a namespace ownership. There is no special relation between a class and its static fields.

You may also say the object referred to by the out variable belongs to the PrintStream class because it is an instance of that class (or a subclass), just as "beagle" belongs to the "dog" class. This is not standard usage in Java parlance, but it makes perfect sense from the perspective of type theory, where a type is really a set of values, and the value of out "belongs" to that the type / set defined by the PrintStream class.

like image 39
6 revs, 3 users 62% Avatar answered Sep 28 '22 07:09

6 revs, 3 users 62%


System.out is a PrintStream object. It is defined in System (so, it is member of System) class as :

static PrintStream out

See: http://docs.oracle.com/javase/6/docs/api/java/lang/System.html

like image 21
Wayan Wiprayoga Avatar answered Sep 28 '22 07:09

Wayan Wiprayoga


It is defined as:

static PrintStream  out 
like image 45
anubhava Avatar answered Sep 28 '22 07:09

anubhava