This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes System.out.println Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "System.out.println" Watch "System.out.println" New topic
Author

System.out.println

Keith Lockey
Greenhorn

Joined: Jan 18, 2003
Posts: 20
I understand that out is a static variable or field of type PrintStream in the System class. Println is a public void method in the PrintStream class.
I don't understand fields very well. Does the fact that the field out is static insure that the method println is static as well?
Could anyone show other examples that might help my confusion?
Jasper Vader
Ranch Hand

Joined: Jan 10, 2003
Posts: 284
Originally posted by Keith Rockey:
[QB]Does the fact that the field out is static insure that the method println is static as well?
QB]

in the sense that a static method is referring to a static variable?


giddee up
Keith Lockey
Greenhorn

Joined: Jan 18, 2003
Posts: 20
PrintStream.println() is not a static method.
But in the System class, a static PrintStream field called out is declared.
I thought a method had to be static in order for a class like System to call it, just like every main is static in order for the JVM class to call it.
I just don't understand how the println is static?
(if my question makes any sense?)
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
The System class doesn't call the println method.
out is a static member of the System class. out is only a reference (aka identifier or variable) that refers to an object.
That object that out refers to is of type PrintStream. Objects of type PrintStream have various behaviors (aka methods) including all those overloaded println methods. The println methods are not static. They are behaviors that belong to a PrintStream object.
Making sense?


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Keith Lockey
Greenhorn

Joined: Jan 18, 2003
Posts: 20
Thanks for the help.
So the only thing that is static is the field out, right. The println method is not static?
Keith Lockey
Greenhorn

Joined: Jan 18, 2003
Posts: 20
so when a class, object or class is static and referred to in a program, it can use any of the methods within it whether or not they are static.
Keith Lockey
Greenhorn

Joined: Jan 18, 2003
Posts: 20
I think Sun has made an error about this. Please see the paragraph at the link I have below.
Thanks,
Keith
----------------------------------------------
class ExampleProgram {
public static void main(String[] args){
System.out.println("I'm a Simple Program");
}
}

"The static fields and methods of a class can be called by another program without creating an instance of the class. So, just as the Java VM interpreter command could call the static main method in the ExampleProgram class without creating an instance of the ExampleProgram class, the ExampleProgram class can call the static println method in the System class, without creating an instance of the System class. "
from
http://developer.java.sun.com/developer/onlineTraining/Programming/BasicJava1/prog.html
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
So the only thing that is static is the field out, right. The println method is not static?
Right.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
so when a class, object or class is static and referred to in a program, it can use any of the methods within it whether or not they are static.
Nope. A static class method may use anything that is local to that method or is also static (such as another static method or a static class field (aka variable or identifier)). A static method may not "directly" make use of instance methods or fields.
To reiterate perhaps more clearly: A static method may make use of a static variable (reference) that refers to an actual object. Through this static variable, instance methods of the referred object could be invoked (from within the context of the static method).
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
I think Sun has made an error about this.
Nope. -Wait! Don't listen to me on this. I didn't read the whole paragraph very carefully. Ilja points out the error below.
Don't feel too frustrated. Learning to figure out how to think in an object-oriented manner while understanding the difference between the world of instances and the static world is not the most intuitive thing at first.
You're figuring it out.
[ January 20, 2003: Message edited by: Dirk Schreckmann ]
Keith Lockey
Greenhorn

Joined: Jan 18, 2003
Posts: 20
Thanks, I think it is becoming clearer.
Keith Lockey
Greenhorn

Joined: Jan 18, 2003
Posts: 20
so println() is static in System.out.println() even though I can't find it declared static in the Java 1.4 api.
Where do I find it?
Keith Lockey
Greenhorn

Joined: Jan 18, 2003
Posts: 20
I was able to change my name this time to real name.
Sorry about the confusion
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Dirk Schreckmann:
I think Sun has made an error about this.
Nope.

Dirk, take a second look...
the ExampleProgram class can call the static println method in the System class, without creating an instance of the System class.

That's certainly horribly misrepresented, isn't it?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
There seems to be a lot of confusion in this thread about what the keyword "static" means.
In the case of System.out, it means that you do not need to create an instance of the System class to access the out object. This is completely independent from whether or not println() is static or not.
Since out is an instance of PrintStream (or one of its decendants perhaps), println() does not need to be static. The static keyword only means you do not require an object to call a static method. However, you CAN use an object to call it, if you wish. In the context of this question, you cannot tell whether or not println() is static. If you look at the API, however, you will see that it is not.
HTH
Layne


Java API Documentation
The Java Tutorial
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Keith Rockey:
"the static println method in the System class"

And yes, this is incorrect. The prinln() method isn't even in the System class, let alone static. The reference out is the static member of System that allows you to do this.
Keith Lockey
Greenhorn

Joined: Jan 18, 2003
Posts: 20
from jdk1.4 source --- code declared in the System class

just trying to figure out this code?
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
It looks to me that if the system clock is set prior to Jan 1, 1970 UTC that a NullPointerException will be thrown.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Keith Lockey
Greenhorn

Joined: Jan 18, 2003
Posts: 20
Why is println() non static in the static out.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Dirk, take a second look...
Oops. I didn't read the whole paragraph carefully enough. Ilja's right, of course (such a statement hardly has to be uttered to be understood as truth). Glad you're around, Ilja.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
The println methods as defined in the PrintStream class are behaviors of instances of PrintStream. So, objects of type PrintStream have these overloaded println behaviors.
These println methods are not defined to be static class methods as the keyword static was not used in their declarations (that's why println() is not static in the static out). So, these behaviors only exist for actual objects (instances) of type PrintStream and they don't exist without such an object (if you'll allow me to be a little sloppy in my terminology).
out is just a reference (a variable) that can be used to refer (point) to an object of type PrintStream (or out could be a null reference).
So, out is just a static reference (pointer) that belongs to the System class. The object that it points to is not static. The behaviors of such an object are also not static.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: System.out.println
 
Similar Threads
System.out.println( );
System.out.println()
dout on static imports
System.out.println
System.out.println