System.out.println() - help pls


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: System.out.println() - help pls

  1. #1
    Join Date
    Mar 2006
    Posts
    12

    System.out.println() - help pls

    pls some one explain me wat is the exact working of this System.out.println() i mean the java has be coded as

    public final static PrintStream out = nullOutputStream();

    this is present in the public final class System

    my ques is y is this out final..?? and wat is that System.out.println() ???

    y is it so.. to call a non static method it should be obj.method() if static it should be classname.method() ... but this is different.. System is a class and out is PrintStream's reference object or it can also be called as Printstream variable..

    so y is that System.out.mtd() .........??? pls some one help me out.. i am badly stuck in this doubt...

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    Your question is pretty jumpy. Do you think you can relax your question a bit so that it is clearer?

  3. #3
    Join Date
    Mar 2006
    Posts
    12
    hey how can i simplify furthur.. ??? i am much more clear.. that it has been coded in the java.lang.System as public final static PrintStream out=nullOutputStream(); thus it is clear that it is out is a static variable of printStream class.. how is the method call done here.. that is how is the println method is being called using System.out.

    we have been calling methods from obj.mtd() or classname.mtd() how is this classname.obj.mtd() how is this accompolished??????

  4. #4
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    out is final so that you can't do System.out = somePrintStream;. It's static because the System class is final, and therefore can't be instantiated; if out wasn't static, then it wouldn't be accessible.

    The out variable is an instance of the PrintStream class; you can do System.out.methodInThePrintStreamClass();. println() and print() are methods in the PrintStream class, so they can be called by using System's out variable.

    Hope this clears things up a bit

  5. #5
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    Quote Originally Posted by kingvignesh
    we have been calling methods from obj.mtd() or classname.mtd() how is this classname.obj.mtd() how is this accompolished??????
    It looks weird because we don't come across public variables often. The Rectangle and Point classes have public variables (look at the APIs), but these variables are primitive types, not references, so we can't invoke methods on them.

    You can actually have SomeClass.a.b.c.d(); if SomeClass is a class, if a was a public static object in SomeClass, b was a public object in a's class, c was a public object in b's class, and d was a public method in c's class.

  6. #6
    Join Date
    Mar 2006
    Posts
    12
    wat is the use of this someclass.a.b.c.d(); is there any other way for us to access the d() in any other manner????

    and y cant we directly use println by creating an obj for printstream class ?? wat is the need of that out obj..?? and that too with all these complexities...

  7. #7
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    [QUOTE=kingvignesh]wat is the use of this someclass.a.b.c.d(); is there any other way for us to access the d() in any other manner????[/code]
    If you create an instance of c.

    Quote Originally Posted by kingveignesh
    and y cant we directly use println by creating an obj for printstream class ?? wat is the need of that out obj..?? and that too with all these complexities...
    Well, you can.
    Code:
    
    PrintStream ps = new PrintStream(System.out);
    ps.println("hello");
    But we are still using System.out. This is because System.out is the standard output stream; it will write to the console. Just like if we are reading from the console with BufferedReader we use System.in, the standard input stream.
    Code:
    
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    On a side note, please try to use correct spelling, punctuation and grammer when posting. It is very hard to understand what you are asking, and more people will try to help you because they will take you more seriously. There is no excuse for typing 'y' instead of 'why', or 'wat' instead of 'what'. There are many people on these forums that do not speak english well, and you do not want to make it any harder for them to learn. Also, one question mark will do.

  8. #8
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    I have some quibble with the supplied explanations for "static" and "final":

    Final means that the class cannot be used as a base class for "children" - you cannot inherit from a final class.

    "Static" refers to fields or methods which can be called/used without having an instance of the class created. These are loaded into "static" memory area when the program is being executed, so they can be called from anywhere by anyone. Classes are not static - methods or fields are static. Static methods or fields are always freely available to any process which can call the class containing that method or field, so are as if they are "public" ...

    "out" is a field of the FilteredOutputStream class - it is the name of the output stream which is being filtered. Since PrintStream derives from FilteredOutputStream, it inherits this field from FilteredOutputStream. The System class has a static field member named "out" which is an object of class PrintStream.

    "println()" is a method of the PrintStream class, which formats and passes the content of the argument to the out stream and adds a "new line" at the end of the stream.

    So, let's put it together: the System class is final;
    it has a static data member of class PrintStream named out;
    out has a method called println() which we get access to statically because the out object is static, which outputs to the chosen System output stream the content it receives as its parameter.

  9. #9
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    Quote Originally Posted by nspils
    Final means that the class cannot be used as a base class for "children" - you cannot inherit from a final class.
    Sorry -- I meant to say that, then I meant to follow that saying that it has a private constructor.

    Quote Originally Posted by nspils
    "Static" refers to fields or methods which can be called/used without having an instance of the class created. These are loaded into "static" memory area when the program is being executed, so they can be called from anywhere by anyone. Classes are not static - methods or fields are static. Static methods or fields are always freely available to any process which can call the class containing that method or field, so are as if they are "public" ...
    Did I say otherwise? And who said anything about static classes?

    Quote Originally Posted by nspils
    "out" is a field of the FilteredOutputStream class - it is the name of the output stream which is being filtered. Since PrintStream derives from FilteredOutputStream, it inherits this field from FilteredOutputStream. The System class has a static field member named "out" which is an object of class PrintStream.
    Correction: FilterOutputStream. And also, FilterOutputStream inherits out from OutputStream.
    Last edited by destin; 03-05-2006 at 02:11 PM.

  10. #10
    Join Date
    Mar 2006
    Posts
    12
    i am extremely sorry.. since i am a newbie to this forum i was not aware of those things.. i am sorry for doing that. i wont repeat this in the mere future..

    and by the way thank you so much..

    but still

    PrintStream ps = new PrintStream(System.out);
    ps.println("hello");

    this code is not working.. jus now i compiled

  11. #11
    Join Date
    Mar 2006
    Posts
    12
    what is the use of this someclass.a.b.c.d(); is there any other way for us to access the d() in any other manner????

    You can actually have SomeClass.a.b.c.d(); if SomeClass is a class, if a was a public static object in SomeClass, b was a public object in a's class, c was a public object in b's class, and d was a public method in c's class.

    this confuses even more.. hey i couldnt get this.. if a is a public static obj of someclass and how come b a public object of a.. because a is an object.. how suddenly a became a class. and wat about the c. ?????????????

  12. #12
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    Quote Originally Posted by kingvignesh
    PrintStream ps = new PrintStream(System.out);
    ps.println("hello");
    Make sure you are importing java.io.PrintStream.

  13. #13
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    Quote Originally Posted by kingvignesh
    this confuses even more.. hey i couldnt get this.. if a is a public static obj of someclass and how come b a public object of a.. because a is an object.. how suddenly a became a class. and wat about the c. ?????????????
    Code:
    
    public class MyClass {
        public static void main(String[] args) {
            SomeClass.a.b.c.d();
        }
    }
    
    class SomeClass {
        public static AClass a = new AClass();
    }
    
    class AClass {
        public BClass b = new BClass();
    }
    
    class BClass {
        public CClass c = new CClass();
    }
    
    class CClass {
        public void d() {
            System.out.println("d()");
        }
    }
    Quote Originally Posted by kingvignesh
    what is the use of this someclass.a.b.c.d(); is there any other way for us to access the d() in any other manner????
    Code:
    
    public class MyClass {
        public static void main(String[] args) {
            CClass c = new CClass();
            c.d();
        }
    }
    
    class SomeClass {
        public static AClass a = new AClass();
    }
    
    class AClass {
        public BClass b = new BClass();
    }
    
    class BClass {
        public CClass c = new CClass();
    }
    
    class CClass {
        public void d() {
            System.out.println("d()");
        }
    }
    Last edited by destin; 03-05-2006 at 03:12 PM.

  14. #14
    Join Date
    Mar 2006
    Posts
    12
    thank you so much destin... is there any specific use by using this type????

  15. #15
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    Quote Originally Posted by kingvignesh
    thank you so much destin... is there any specific use by using this type????
    What?

Similar Threads

  1. Replies: 0
    Last Post: 01-10-2002, 01:43 AM
  2. thread problem....pls help
    By Satish in forum Java
    Replies: 0
    Last Post: 11-29-2001, 09:07 AM
  3. class path problems pls help
    By sunil in forum Java
    Replies: 1
    Last Post: 06-13-2001, 09:03 AM
  4. Replies: 1
    Last Post: 06-05-2001, 06:12 AM
  5. PLS CHECK THE CODE
    By SATISH in forum Java
    Replies: 1
    Last Post: 08-30-2000, 12:37 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center