wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes basic question about static initializer block Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "basic question about static initializer block " Watch "basic question about static initializer block " New topic
Author

basic question about static initializer block

mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
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);
}
}
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
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


Rob
SCJP 1.4
mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
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

Joined: Jan 07, 2002
Posts: 2205
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
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
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

Joined: Jan 07, 2002
Posts: 2205
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

Joined: Nov 04, 2001
Posts: 1871
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: basic question about static initializer block
 
Similar Threads
Initializers
initialization sequence
a simple question
Please explian the output ....
Order of Execution(Interesting)-:)