• 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

why only println() method invokes by System.out.println() unlike other methods invokation

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Expert,

why only println() method invokes by System.out.println() unlike other methods invokation?
 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi samir

What exactly do you mean?

The println() method is invoked on the static member out (of type PrintStream). This member is public within the System class which means it can be accessed using System.out.
 
samir vasani
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

James Boswell wrote:Hi samir

What exactly do you mean?

The println() method is invoked on the static member out (of type PrintStream). This member is public within the System class which means it can be accessed using System.out.


Normally methods in Java can be called by using instance(if the method is non static ) and using class name if method is static .But in case of println() method i use class name "System" then object "out"
and then "println()" means classname then object and then meth.d name and not by instance name.So thats my question is.
 
Ranch Hand
Posts: 115
11
IntelliJ IDE Clojure Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure where the confusion lies. You seem to have answered your own question! I'll try again in different words, though. Maybe that'll help

As James explained, the println() method is a method of the PrintStream class, and out is an instance of PrintStream. So, imagine we had created out own object and called println() on it, like this:



So, in this case, what you said is exactly true: methods in Java can be called by using an instance.

Again, as James explained, the System class contains a public static field called out:



If we wanted to access out from another class, we could use System.out to get that reference, correct? As you said: you can use the class name if the method [or field] is static. Now that we have a reference to our PrintStream object (System.out), we can do things with it by calling it's methods. We want to print something, so we call println() on the object. All together, that's System.out.println();

You can imagine it broken into steps, if that helps make things clearer:



Don't do that, though. ;)
reply
    Bookmark Topic Watch Topic
  • New Topic