• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

forward referencing for static variable??

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because all instance and class variables are provided with a default Value. If you run the program, the output is 0.
 
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well I think the trick is:
static int p = abc();
is declaration + initialization to the variable, you should look at at as:
static int p;
p = abc();
so my guess is that once the compiler see the declaration part, it gives the default value to the p as it's a member variable, then it does the evaluation of abc() and then assign it to p.
When abc() is executed, the value of x is already set to the default (0). if you add a line in the main method, p = abc(); it will print 0 then 123.
if you want to see it clear, then make abc()
static int abc(int i){
System.out.println(p);
return i;
}
pass 2 in the first call and 3 in the second and it will print 0 then 2
hope this help.
 
Richa Jeetah
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanx, now its seems more clear
reply
    Bookmark Topic Watch Topic
  • New Topic