wood burning stoves*
The moose likes Beginning Java and the fly likes How does System.out.println (Works) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How does System.out.println (Works)" Watch "How does System.out.println (Works)" New topic
Author

How does System.out.println (Works)

faisal usmani
Ranch Hand

Joined: Jan 14, 2006
Posts: 139
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
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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 ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
faisal usmani
Ranch Hand

Joined: Jan 14, 2006
Posts: 139
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 ]
Kj Reddy
Ranch Hand

Joined: Sep 20, 2003
Posts: 1704
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.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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?


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How does System.out.println (Works)