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

basic question about static initializer block

 
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why does this give a compile error:
undefined variable r in System.out.println(r)
what is the rule for the static initializer block here ?
public class Test {
static {int r = 7;}
public static void main(String args[])
{
System.out.println(r);
}
}
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by mark stone:
why does this give a compile error:
undefined variable r in System.out.println(r)
what is the rule for the static initializer block here ?
public class Test {
static {int r = 7;}
public static void main(String args[])
{
System.out.println(r);
}
}



In your static block, you create a local variable named r. When the static block completes, the variable r goes out of scope and no longer exists.
Try this instead:
public class Test {
static int r;
static { r = 7;}
public static void main(String args[])
{
System.out.println(r);
}
Rob
 
mark stone
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok. so basically i got to reread the static initializer block. as far as i can recollect static initializer blocks are run by first when class is loaded. right ? i did not know that the variables inside such blocks are local.

Originally posted by Rob Ross:


In your static block, you create a local variable named r. When the static block completes, the variable r goes out of scope and no longer exists.
Try this instead:
public class Test {
static int r;
static { r = 7;}
public static void main(String args[])
{
System.out.println(r);
}
Rob

 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any time you declare a variable in a block, it is local to that block.
public void methodFoo()
{
int i; //local to methodFoo()
while(i!=0)
{
String str; //local to this while loop
}
{
double x; //local to this block within the curly braces.
}
}
Rob
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well,
LOCAL VARS are LOCAL in anycase. it is not partial to any container context. why should it be?
whenever u have some enclosing block with {} and within that if u have declared some var then it's scope is surely that block only. it's life time may be longer though sometimes.
e.g. local inner classes like the following example,

Here, hoders array's elements are of class MyIntHolder type but the class is local to the for loop. still the life time is longer than just that for loop as we store it's instances in the array of IntHolders.
so, we cant create instance of the MyIntHolder outside the declaring for loop but use the created instances outside it.

regards
maulin.
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes but the question wasn't about the lifetime of an object, it was about the lifetime about a variable declared in a block
If you had defined holder within the body of the for-loop, it wouldn't be in scope for the second for loop, even though the objects created in the first for loop lived on.
Rob
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah... the question wasnt about life time but i just mentioned it to make it more clear but it made it more confusing
regards
maulin.
 
reply
    Bookmark Topic Watch Topic
  • New Topic