Results 1 to 5 of 5
- 01-16-2012, 09:14 PM #1
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
How does System.out.println() method work?
What I see is that "out" is a static class variable in System class of type PrintStream. ( from the API )...
Since println() found in PrintStream class is not a static method, you need an object to call its method. ( You can not just say PrintStream.println("Hello World..")...
System class has a reference (PrintStream out) in its fields. But where is the object?
I will answer my own question:
Java Code:package myPackage; public class MyPrintStreamClass { public void bark() { System.out.println("Bark!"); } }Java Code:package myPackage; public class MySystemClass { public static final MyPrintStreamClass outt = new MyPrintStreamClass(); }( This is for discussion and helping purposes. )Java Code:package myPackage; public class MyTestClass { public static void main(String[] args) { MySystemClass.outt.bark(); } }
//
You can also do something like this:
What you are doing here is:Java Code:PrintStream consoleOutputStream = new PrintStream(System.out); consoleOutputStream .println("Hello");
Defining a variable pointing to a PrintStream object,
that is a copy of the object that is in System Class.
So the field out in System class, which is type of PrintStream is already pointing to an Object.
- 01-17-2012, 09:53 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
- 01-17-2012, 10:15 AM #3
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Re: How does System.out.println() method work?
So consoleOutputStream is referencing a PrintStream object, that we have created by saying new PrintStream().
and what we are passing into the PrintStream() constructor is: System.out
Which is a reference itself, that is pointing to an already existing PrintStream Object.
which means:
consoleOutput --> A PrintStreamObject(System.out --> Already Existing Object)
- 01-17-2012, 11:03 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: How does System.out.println() method work?
That's correct.
- 01-17-2012, 11:04 AM #5
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Similar Threads
-
(i%5=0)?System.out.println():System.out.print(" "); does not work!
By oszc in forum New To JavaReplies: 14Last Post: 08-07-2011, 09:25 AM -
is System.out.print same as System.out.println ?
By usuf in forum New To JavaReplies: 8Last Post: 06-21-2011, 02:21 PM -
Println VS system.out.println
By ccie007 in forum New To JavaReplies: 2Last Post: 05-20-2010, 08:52 AM -
difference between system.out.println() & out.println()
By wickedrahul9 in forum Advanced JavaReplies: 5Last Post: 10-18-2008, 11:06 PM -
System.out.println
By Sniper-X in forum Advanced JavaReplies: 10Last Post: 05-05-2008, 03:41 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks