Hi,
Can you please explain why the answer is 4 and not 1?
as i understant at line #7 we try to print value of p which should be intialised only after the return atatement at line #8.
tough i know that static variable are initialized automatically(if not given an initializing value, like if it were static int p; at line#3)
...BUT are they intialized automatically even if we specify..as on line#3 ?
1 class Q30
2 {
3 static int p=abc();
4 static public int abc()
5 {
6 int i=123;
7 System.out.println(p);
8 return i;
9 }
10 public static void main(
String arg[])11
12 {
13 }
14 }
1. Compile time error for forward referencing the variable p at line no. 7.
2. Run time error for forward referencing the variable p at line no. 7.
3. Program compiles correctly and prints 123 when executed.
4. Program compiles correctly and prints 0 when executed.