• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

static variable -- confusion?

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Below is the prgram

class Test{
static int j = 100;
static{
j = 200; // line 1
i = 500; // line 2
i = i + 1; // line 3
System.out.println(j+":value:"+i); // line 4
}
static int i = 200;
}

My assumption is error to be raised at line 2.
But error is raised at line 3.


Please let me know wky..
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Badri
Your code will produce forward reference error.

class Test{
static int j = 100;

static{
j = 200; // line 1
i = 500; // line 2
i = i + 1; // line 3
System.out.println(j+":value:"+i); // line 4
}
static int i = 200;
}

There wont be error in line 2, because since i is in static block(it means that declaration part is there somewhere). Error is shown in line 3 as i value is being used before the declaration line.

If you change the code as
class Test{
static int j = 100;
static int i = 200;
static{
j = 200; // line 1
i = 500; // line 2
i = i + 1; // line 3
System.out.println(j+":value:"+i); // line 4
}

}

It works fine.

If any thing is wrong, please let me know

Bye
 
Badri Sarma
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Vinod for the reply. But still i have a doubt....

Originally posted by vinod balaji:

There wont be error in line 2, because since i is in static block(it means that declaration part is there somewhere).



Why the i = i + 1, cannot think in the same fashion as you mentioned "it means that declaration part is there somewhere" and compile the code.
 
vinod balaji
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is the forward reference method.

Eg:
a=1; // here a will be assigned to 1
a=a+1; // here a will be incremented and then assign


In that program(Your Program), assigning gives no problem, it will not allocate memory until it finds the declaration, but while u use a=a+1, it will see the right hand side value(a+1) where it will think that value hasnt been declared and it will issue forward reference error

i think it is clear, if u still have doubt reply me.
 
Badri Sarma
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vinod Thanks for replying to my queries.
But still doubt persist in my mind it is not 100% clear.
Can you brief me on "forward reference error".
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Forward Referencing
 
It's fun to be me, and still legal in 9 states! Wanna see my tiny ad?
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic