posted 17 years ago
Found this question on a mock:
Question 38.
Given the code, what is the output
A.Code Does't Compile.
B.Code Compiles but error will be generated at runtime.
C.Code Compiles & runs, output is
i is : 10
D.Code compiles & runs, output is
i is : 20
Ans is A. The code does not compile.
However if I replace line line 4 with
int i = 20; it compiles fine and runs with output "i is 20".
Also if I make both Line 2 and Line 4 non-static, it compiles and runs with output "i is 20".
Lastly if I make Line 2 non static, and 4 static it again fails to compile.
Am I right in saying that:
1. Static variables cannot be shadowed by another static variable. But it can be shadowed by a non-static variable.
2. Non Static variables can be shadowed by another non-static variable, but not by a static variable.
Monisha.
Hey! It compiles! Ship it!