File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How does System.out.println (Works)

 
faisal usmani
Ranch Hand
Posts: 139
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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 ]
 
Kj Reddy
Ranch Hand
Posts: 1704
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic