• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

System.out.println

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Keith Lockey
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?)
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Keith Lockey
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So the only thing that is static is the field out, right. The println method is not static?
Right.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, I think it is becoming clearer.
 
Keith Lockey
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was able to change my name this time to real name.
Sorry about the confusion
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
from jdk1.4 source --- code declared in the System class

just trying to figure out this code?
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks to me that if the system clock is set prior to Jan 1, 1970 UTC that a NullPointerException will be thrown.
 
Keith Lockey
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why is println() non static in the static out.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic