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

7

5

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.

Riding answered 16/5, 2013 at 14:48 Comment(2)
You could check it via System.out instanceof PrintStream?System.out.println("Ok, I'm PrintStream"):System.out.println("No, I'm not aPrintStream")Acidity
Define 'belong to'. It has no meaning in Java. System.out is a member of System and it is of type PrintStream.Steam
M
7

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

Monacid answered 16/5, 2013 at 14:50 Comment(0)
M
5

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.

Maryannamaryanne answered 16/5, 2013 at 14:49 Comment(3)
Community wiki answer?Gwenngwenneth
@LuiggiMendoza See, I liked the idea!Maryannamaryanne
Great then. By the way, I would remark the last sentence: There is no special relation between a class and its static fields.Gwenngwenneth
R
3

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

Reviviscence answered 16/5, 2013 at 14:50 Comment(0)
S
2

It is defined as:

static PrintStream  out 
Shrovetide answered 16/5, 2013 at 14:50 Comment(0)
I
2

out is a static field in System. Its type is PrintStream.

Institutionalism answered 16/5, 2013 at 14:51 Comment(0)
M
0

In loose simple terms, 'out' is a field (i.e. an object) in class 'System' of type 'PrintStream'.

Mistress answered 16/5, 2013 at 14:51 Comment(0)
T
0

Out is the static reference variable in class System and is of PrintStream(class) type.

Tomkin answered 16/5, 2013 at 15:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.