What is the output of the code? | Sololearn: Learn to code for FREE!
+ 1

What is the output of the code?

class Test{ public static void main(String[] args){ System.out.print(this); } }

24th Mar 2017, 1:54 AM
Bhavesh Joshi
Bhavesh Joshi - avatar
2 Answers
+ 8
Error. It would not output "this" because "this" is not defined as a string by using "". If test were a variable (which no variable was initialized) then it would print the value contained in that variable. You either need to initialize it as a variable or use quotation marks to specify that it is text that should be printed.
24th Mar 2017, 2:00 AM
J.G.
J.G. - avatar
+ 2
Actually u get compile error: non-static variable this cannot be referenced from a static context System.out.print(this); ^ 1 error 'this' is a key word reference to the current object. As Compiler error message shows u can't call non static method/variable in static method.
24th Mar 2017, 2:31 AM
Eranga
Eranga - avatar