1

The text book has it mentioned as:

System.out -> Class

Println -> Method

But, I disagree.

I think it's:

System -> Class

Out -> Method

Now, according to my logic, should Println be a method inside the method (Out)?

What is the correct concept here?

Community
  • 1
  • 1
B-Mac
  • 557
  • 2
  • 14

3 Answers3

1

System.out isn't a class or a method, it's a static field on the System class that is a reference to an instance of a class (PrintStream). println is an instance method of PrintStream (or more completely, println is the common name shared by a group of instance methods that are overloaded with different arguments).

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Wait. It's take it one step at a time here. So, is `System.out` a static field of the `System` class or is `out` a static field of the `System` class. I think the latter but you say the former. – B-Mac Mar 08 '15 at 18:37
  • @Grendan: Those are the same thing, it's just that I partially-qualified the name (`System.out`) and you're using the unqualified name (`out`); it's the same field regardless. Or one could go whole-hog and *fully* qualify the name and say "`java.lang.System.out` is a static field of the `java.lang.System` class..." – T.J. Crowder Mar 08 '15 at 18:37
  • I see. Thank you. Can you please explain how it's a reference to an instance of the `PrintStream` class please? – B-Mac Mar 08 '15 at 18:42
  • @Grendan: Continue with your Java learning and the terms "instance" and such will become clear to you. It's far too large a topic to go into in an SO answer. Or possibly not, as [there seem to be useful answers to "What exactly is an instance?"](http://stackoverflow.com/questions/5126082/what-exactly-is-an-instance-in-java). – T.J. Crowder Mar 08 '15 at 18:45
  • Actually, I do know what an instance is and all that. In fact, I just finished inheritance. I'm good with the basics. Just that we've been using System.out.println all the time without understanding what's going on behind the scenes. That's why I asked. – B-Mac Mar 08 '15 at 18:57
  • What is the relationship between the System class and the PrintStream class? – B-Mac Mar 08 '15 at 18:59
  • @Grendan: Pretty much the `out` and `err` fields. That is, the `System` *uses* the `PrintStream` class. – T.J. Crowder Mar 09 '15 at 07:01
0

System is a class. Out is an object (public static final PrintStream out). println and print are methods. Trust the textbook.

Proof

lacraig2
  • 655
  • 4
  • 18
  • I disagree. If System is a class and out is an Object, System.out makes no sense. – B-Mac Mar 08 '15 at 18:44
  • Out is an instance of PrintStream. Here is the hierarchy. System is a class. System has an instance (object) called Out of PrintStream The PrintStream class has a method called print() and println(). You can call those through System.out.println(); I realize that this is new and you likely don't understand yet, but once you get into more difficult OOP you will understand the class structure better. For now trust all of the people on here telling you the correct answer (and the official Java documentation I posted). – lacraig2 Mar 08 '15 at 18:46
0

Methods cannot contain other methods, plus "out" never has any parentheses or takes any arguments, so "out" cannot be a method.

Chuck Quinn IV
  • 186
  • 1
  • 11