This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
Regarding the output, you will get compilation error, since you are trying to use s before initializing it.(Local variables need to be initialised before use)
Gunjaan Agarwal
Greenhorn
Joined: Jun 07, 2006
Posts: 11
posted
0
gives u compilation error as non-static field is being accessed in static method
joshua antony
Ranch Hand
Joined: Jun 05, 2006
Posts: 117
posted
0
oh sorry the output is null.
Gunjaan Agarwal
Greenhorn
Joined: Jun 07, 2006
Posts: 11
posted
0
its not a local variable..its an instance variable..compilation error is coz no class instance is used to access s
Bonny Sen
Greenhorn
Joined: May 09, 2006
Posts: 26
posted
0
But the code will run if you use System.out.println("Output: " + o.s); else it will give a compiler error as a non-static variable cannot be referenced from a static context
With Love, Bonny
"Life is like an ice-cream, Enjoy it before it melts..."
Shiaber Shaam
Ranch Hand
Joined: Jun 16, 2006
Posts: 252
posted
0
I may be wrong in asking that qn.. let me review...
public class ObjectTest { public static void main(String args[]) { String s=null; System.out.println(s); } }
Now i am clear with my qn.. i want to clarify whether it gives nullptrexcpn or just prints null... and with reasons....
Amit Goyal
Ranch Hand
Joined: Feb 21, 2006
Posts: 95
posted
0
when u prints an String object through System.out.println() method, toString() method is called. Since there is no object i.e. String = null ; you will get NullPointerException.
Regards,
Amit Goyal
Gunjaan Agarwal
Greenhorn
Joined: Jun 07, 2006
Posts: 11
posted
0
it will print null amit.Does not give a exception.An exception occurs when u don initaialize it.However if it was a instance var n not local u need not initialize it as instance var are automatically initialized.
S Thiyanesh
Ranch Hand
Joined: Mar 19, 2006
Posts: 142
posted
0
null will be printed
Shiaber Shaam
Ranch Hand
Joined: Jun 16, 2006
Posts: 252
posted
0
i want clarifications that it will give nullptrexcpn or prints null.. and also WHY???
Shiaber Shaam
Ranch Hand
Joined: Jun 16, 2006
Posts: 252
posted
0
i want clarifications that it will give nullptrexcpn or prints null.. and also WHY???
Amit Goyal
Ranch Hand
Joined: Feb 21, 2006
Posts: 95
posted
0
oopss! Gunjaan ur right. Once i gone through the internal implementation of println() method i got the ans.
Thanx for correcting me!
Following the impl:
Regards,
Amit Goyal
Bonny Sen
Greenhorn
Joined: May 09, 2006
Posts: 26
posted
0
The variable String s is an instance of the class ObjectTest and it has not been explicitly initialized. Sowhen the class loads then s would be initialized to its corresponding default value, whic is null for strings. So null is printed when u call it as: System.out.println("Output: " + o.s);
wise owen
Ranch Hand
Joined: Feb 02, 2006
Posts: 2023
posted
0
The PrintStream's "public println(Object x)" calls its "public void print(Object obj)" to print an object. The string produced by the "String.valueOf(Object obj)" method is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.
Here is the implementation of String.valueOf(Object obj):
In your case, null string will be printed.
Shiaber Shaam
Ranch Hand
Joined: Jun 16, 2006
Posts: 252
posted
0
Here... Everything gets cleared... Thank U all.....
Change your display names to meet the JavaRanch Naming Policy of using your real first and real last names. Failing to comply can result if closing accounts.