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.
It depends what you mean by "belongs to".
Usually, people would say
out
"belongs" toSystem
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 thePrintStream
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 ofout
"belongs" to that the type / set defined by thePrintStream
class.