Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

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...
[692 byte] By [kingvignesh] at [2007-11-11 8:09:41]
# 1 Re: System.out.println() - help pls
Your question is pretty jumpy. Do you think you can relax your question a bit so that it is clearer?
nspils at 2007-11-11 22:36:13 >
# 2 Re: System.out.println() - help pls
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??
kingvignesh at 2007-11-11 22:37:18 >
# 3 Re: System.out.println() - help pls
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
destin at 2007-11-11 22:38:16 >
# 4 Re: System.out.println() - help pls
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.
destin at 2007-11-11 22:39:20 >
# 5 Re: System.out.println() - help pls
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...
kingvignesh at 2007-11-11 22:40:25 >
# 6 Re: System.out.println() - help pls
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=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.

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.

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.
destin at 2007-11-11 22:41:18 >
# 7 Re: System.out.println() - help pls
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.
nspils at 2007-11-11 22:42:27 >
# 8 Re: System.out.println() - help pls
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.

"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?

"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.
destin at 2007-11-11 22:43:24 >
# 9 Re: System.out.println() - help pls
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
kingvignesh at 2007-11-11 22:44:27 >
# 10 Re: System.out.println() - help pls
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. ?????
kingvignesh at 2007-11-11 22:45:22 >
# 11 Re: System.out.println() - help pls
PrintStream ps = new PrintStream(System.out);
ps.println("hello");
Make sure you are importing java.io.PrintStream.
destin at 2007-11-11 22:46:25 >
# 12 Re: System.out.println() - help pls
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. ?????

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()");
}
}

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??

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()");
}
}
destin at 2007-11-11 22:47:23 >
# 13 Re: System.out.println() - help pls
thank you so much destin... is there any specific use by using this type??
kingvignesh at 2007-11-11 22:48:24 >
# 14 Re: System.out.println() - help pls
thank you so much destin... is there any specific use by using this type??
What?
destin at 2007-11-11 22:49:35 >
# 15 Re: System.out.println() - help pls
sorry.. jus explain me what is special of using SomeClass.a.b.c.d() anyway it works fine with c.d() itself from main as your code says...
kingvignesh at 2007-11-11 22:50:28 >
# 16 Re: System.out.println() - help pls
sorry.. jus explain me what is special of using SomeClass.a.b.c.d()
There is no benefit, except in some cases, like System.out. out is special instance of the PrintStream class that prints to the console.
destin at 2007-11-11 22:51:32 >
# 17 Re: System.out.println() - help pls
thank you so much destin for your patience in replying me constantly.
kingvignesh at 2007-11-11 22:52:34 >