I'm a beginner learning Java with some knowledge of C++, and the System.out.println(); is confusing me right now. So System is the class, out is a variable that can call a method?? According to: http://journals.ecs.soton.ac.uk/java/tutorial/getStarted/application/objects.html out is a class variable, and a variable is a storage location in the computer memory that has a type name and content. It's not an object like string that can use methods like .getLength(). The way the website explains it is that out refers to an instance of PrintStream class, but how?
It's not an object
This is where your reasoning is going wrong. System.out
is (a reference to) an object.
The type of the reference is PrintStream
, as documented in the Javadoc. This means that you can call PrintStream
's methods on System.out
, e.g.:
System.out.println();
out
doesn't call a method : out
is a variable holding an object (an instance of PrintStream
) on which you can call a method.
For example :
System.out.println("hey!");
You could also do
void print(PrintStream ps, Object o) {
ps.println(o);
}
...
print(System.out, "hey!");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With