1. public class Example1 {
2.
3. public static void main(String[] args) {
4.
5. System.out.println("To print x you need " + x);
}
}
Which of these following given statement allow this program to compile and execute?
(1) "char x;" placed on line 2
(2) "char x;" placed on line 4
(3) "char x = 'g';" placed on line 2
(4) "char x = 'g';" placed on line 4
(5) "static char x='g';" placed on line 2
(6) "char x = new char();" placed on line 4
(4),(5)
Only statement 4 and 5 allow program to execute successfully and rest fails because of following reasons.
(1) : Cannot make a static reference to the non-static field x
(2): The local variable x may not have been initialized.
(3): Cannot make a static reference to the non-static field x.
(6): Syntax error on token "char", invalid ClassType