wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Static Methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Static Methods" Watch "Static Methods" New topic
Author

Static Methods

Vonique Leary
Ranch Hand

Joined: Mar 24, 2008
Posts: 107
I know this question has been explored alot in the beginner's forum, but I haven't found an answer to the particular question I still have:

If static methods cannot call instance methods and variables, what about the System.out.println() method? I have seen the main method call this method many times. Is the System.out.println() method not an instance method, like my book says?

Thanks, Vonique
Vivek Singh
Ranch Hand

Joined: Oct 27, 2009
Posts: 92
Vonique Leary wrote:If static methods cannot call instance methods and variables, what about the System.out.println() method? I have seen the main method call this method many times. Is the System.out.println() method not an instance method, like my book says?
Thanks, Vonique

NO, the complete signature of out object is:-


So it is a static object already open and ready to accept output data.

And println method siganture is:-


So as you said "static methods cannot call instance methods and variables" it's true.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Code in a static method cannot call an instance method of the same class. It's perfectly okay for code anywhere to call an instance method of some class if it has an instance of that class to call it on.

And that's what's happening there. As Vivek pointed out, "System.out" is a PrintStream object. In other words, it's an instance of the PrintStream class. And therefore it's perfectly normal to call instance methods of that object, for example the "println()" method.
Vonique Leary
Ranch Hand

Joined: Mar 24, 2008
Posts: 107
Thanks, that makes sense. But now I have another question. When you say that System.out is a PrintStream object, can a reference variable be a reference to two objects? I found the following snippet on the web:

"Out is a reference variable or class variable of the system class. It is an object of the System class and therefore a reference variable" (dickbaldwin.com)

And then on another site I found:

"The out variable in the System class refers to (points to) an instance of the PrintStream class (a PrintStream object) which is automatically instantiated when the System class is loaded into the application." (can't remember where I quoted that from).

So, can the out reference variable refer to both the System class AND the PrintStream class? And if so, where can I find the code that documents this? Maybe I'm confused about the phrase, "refers to." What does it mean when a reference variable refers to something? Does it mean it refers back to the class from which it originates? Or does it mean the object type that is returned from the method?

I know I know this already but there are so many different ways to say things in Java that I get confused, especially when I'm learning new concepts.

Thanks,
Vonique
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Vonique Leary wrote:So, can the out reference variable refer to both the System class AND the PrintStream class?


No. But then neither of the quotes you posted said anything like that. In fact the "out" variable doesn't refer to any classes at all and neither of those quotes say it does. The first one doesn't say anything about what it refers to, just that it's a reference variable. The second one says it refers to an instance of the PrintStream class; it doesn't say it refers to the PrintStream class.
Vonique Leary
Ranch Hand

Joined: Mar 24, 2008
Posts: 107
Yes, I get it now. I'm reading it wrong, but your explanation makes sense. Thanks! Totally cleared that up for me.

Von
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Yes, it does take a while to get used to the terminology. Fortunately you were reading quotes there which used correct Java terminology, it's quite possible to find quotes on the Internet which use the terminology carelessly, or even incorrectly. That's just life on the web though.
Vonique Leary
Ranch Hand

Joined: Mar 24, 2008
Posts: 107
Thank you, Paul. I always try to do research on my own first, but you are right. There is so much conflicting information and terminology out there. So, that's when I come here.

Love this board!

Vonique
 
GeeCON Prague 2014
 
subject: Static Methods