Hi My friends:
I have one question from Sreenivasa Kumar Majji's Sun Certified
Java programer practice exam exam 1, still don't understand. Please give me a help. Thanks.
The question is:
1: public class Q8
2: {
3: int i = 20;
4: static
5: {
6: int i = 10;
7:
8: }
9: public static void main(
String[] args)
10: {
11: Q8 a = new Q8();
12: System.out.println(a.i);
13: }
14: }
A) Compilation error, variable "i" declared twice.
B) Compilation error, static initializers for initialization purpose only.
C) Prints 10.
D) Prints 20.
the answer is D.
Another question is:
if a variable is defined and initialized in the class, and the same variable is defined and initialized in a method in the same class. What is the result if I print this variable in the local method or in the class scope?
Haijun