public static void method(){ System.out.println(s + x); } }
1)The code compiles and displays "abcd5". 2)The code compiles, but throws an exception at runtime, because variable x isn't declared as static. 3)The code fails to compile because you can't make a static reference to a non-static variable. 4)The code will compile and display "abcd5" if x is declared to be static. 5)The code will compile by removing the static keyword from the declaration of method(). The code will compile by removing the static keyword from the declaration of method(). I thoguht the answers are 3) and 4), but 5) is also given as correct answer.
Thandapani Saravanan
Ranch Hand
Joined: Oct 17, 1999
Posts: 117
posted
0
3, 4 and 5 are correct answers. How 5? Once the amethod becomes instance (by removing static keyword), it can use instance variables. Only static methods can not use them directly, because there is no instance associated with those methods.