Results 1 to 5 of 5
  1. #1
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default 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();
    }
    Java Code:
    package myPackage;
    
    public class MyTestClass 
    {
    	public static void main(String[] args) 
    	{
    		MySystemClass.outt.bark();
    	}
    }
    ( This is for discussion and helping purposes. )


    //

    You can also do something like this:

    Java Code:
    PrintStream consoleOutputStream = new PrintStream(System.out);
    		consoleOutputStream .println("Hello");
    What you are doing here is:
    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.

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,009
    Rep Power
    20

    Default Re: How does System.out.println() method work?

    Quote Originally Posted by fatabass View Post
    You can also do something like this:

    Java Code:
    PrintStream consoleOutputStream = new PrintStream(System.out);
    		consoleOutputStream .println("Hello");
    What you are doing here is:
    Defining a variable pointing to a PrintStream object,
    that is a copy of the object that is in System Class.
    It's not a copy.

    It is a (pointless) wrapping of the existing PrintStream object from System inside of another PrintStream object.

  3. #3
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default 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)

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,009
    Rep Power
    20

    Default Re: How does System.out.println() method work?

    That's correct.

  5. #5
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

Similar Threads

  1. Replies: 14
    Last Post: 08-07-2011, 09:25 AM
  2. Replies: 8
    Last Post: 06-21-2011, 02:21 PM
  3. Println VS system.out.println
    By ccie007 in forum New To Java
    Replies: 2
    Last Post: 05-20-2010, 08:52 AM
  4. difference between system.out.println() & out.println()
    By wickedrahul9 in forum Advanced Java
    Replies: 5
    Last Post: 10-18-2008, 11:06 PM
  5. System.out.println
    By Sniper-X in forum Advanced Java
    Replies: 10
    Last Post: 05-05-2008, 03:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •