• 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

How does System.out.println (Works)

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

I was just curious to understand how 'System.out.println' works in
java , I tried a code in java but it is giving me a run time exception
please tell me where i am wrong and rectify the code , if possible
___________________________________________________________________________
class prntstrm
{
public prntstrm()
{
System.out.println("Inside constructor prntstrm");
}

public void PRINTLN()
{
System.out.println("Inside prntstrm PRINTLN");
}

}

final class system
{
public static prntstrm OUT;

}


class demo
{
public static void main(String arg[])
{
system.OUT.PRINTLN();
}

}

Exception is ---->
Exception in thread "main" java.lang.NullPointerException
at demo.main(prt.java:28)

___________________________________________________________________________

One more thing (Can we call a static reference variable as an object , as here in this case , can we call OUT as an object of prntstrm
--------------------

regards
faisal
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This little bit:

declares a variable (name is OUT, type is prntstrm) so the compiler says you are good to reference it. But this bit does not give the variable any value. You probably mean to add:


This was a neat little experiment to mimic System.out and see how it all hangs together. I hope it works with that little tweak. But I have to point out that it does a number of dangerous things you don't want to do in real code:

Class names should start with a capital letter, eg Prntstrm

Variable names should start with a lower case letter, eg out

Duplicating names from the Java library will really confuse folks. Make a more distinctive name for your experimental "system".

Keep on experimenting, asking questions and having fun with Java!
[ February 15, 2006: Message edited by: Stan James ]
 
faisal usmani
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the appreciation , just one thing more

(Can we call a static reference variable as an object , as here in this case , can we call OUT as an object of prntstrm ) , if not then that means
'out' of (PrintStream class of java) is instantiated in System class something like this

public static PrintStream out = new PrintStream();

in System class , But according to Herbert Schield (Complete Reference -2 fifth edition (in and out) are stream variables of Input and Output Stream Classes) rather than objects .


Hope you got my point

Regards
[ February 15, 2006: Message edited by: faisal usmani ]
 
Ranch Hand
Posts: 1704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by faisal usmani:
Thanks for the appreciation , just one thing more

(Can we call a static reference variable as an object , as here in this case , can we call OUT as an object of prntstrm )


Regards



Yes we can.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by KJ Reddy:
Yes we can.



Well... no, not really. A variable is a variable, and an object is an object. A variable can refer to an object, or it can be "null", referring to no object at all. "System.out" and "System.in" are, as Mr. Schildt correctly says, are variables of type PrintStream and InputStream, respectively.

But those variables do, indeed, refer to objects. Sometimes, people will say "the object System.out" as an informal way of saying "the object referred to by the variable System.out", but there's still a difference, and it's important to understand it -- because variables can refer to different objects during their lives, and objects may be referred to by different variables, or multiple variables at the same time.

OK?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic