Forums Register Login

static initializer block

+Pie Number of slices to send: Send
can anyone explain me what is the purpose of 'static initializers block'?

when class is created variables which are declared in static block gets initialized.
its compliler error if i try to use that varibles outside of that static block. so inside the block variables are local.

please advise!!
+Pie Number of slices to send: Send
I guess you would be able to do something like this without static initialization block:

public class Demo{
private static final String lookupValue;
static{
try{
lookupValue = PropertyReader.getValue(Constants.LOOKUP_NAME);
} catch (Exception e){
//handle exception
}
}
}
+Pie Number of slices to send: Send
Static blocks mainly used to initialize class variables(static) when ever the class loaded into memory..


static blocks will be executed before your constructor(s)..
+Pie Number of slices to send: Send
following code compiles and runs fine.(I'm not consider Exception in thread main). it works well if static String variable is not final also.

public class Static{
private static final String lookupValue= "main";
static{
try{
//lookupValue = PropertyReader.getValue(Constants.LOOKUP_NAME);
System.out.println(lookupValue);
} catch (Exception e){
//handle exception
}
}
}

Static blocks will be initialized before initializing static variables.---posted by Anand B Raju.

is this above statement true?

thanks.
+Pie Number of slices to send: Send
Freinds,

Following is the order of execution of different statements:

1. Class ios loaded by Class Loader.
2. Static blocks are executed.
3. Static Variables are initialized.
4. Object is created.

Please correct me if I m wrong.
+Pie Number of slices to send: Send
2. Static blocks are executed.
3. Static Variables are initialized.

Above code , how static Stirng variable lookupValue printed out?

confused!!

if i'm missing here anything ,explain.

Thanks.
+Pie Number of slices to send: Send
if u notice 'lookupValue' is a constant which has got a static modifier also..as far as i know constants are resolved at compile time and not runtime.so,it will print the value of 'lookupValue'.

correct me if im wrong
+Pie Number of slices to send: Send
Hi Nibin,

code:

public class Static{
private static String lookupValue= "static variable";
static{
try{
//lookupValue = PropertyReader.getValue(Constants.LOOKUP_NAME);
System.out.println(lookupValue);
} catch (Exception e){
//handle exception
}
}
}

perfectly compiles and runs .
prints : static variable
Exception in thread main--this is not important here.

thanks for you.
+Pie Number of slices to send: Send
+Pie Number of slices to send: Send
"Forward references made on the left hand side of the assignment is always allowed"

I read that thread. its cleared my doubts about static initializer block.

Thanks for your help , Kedar Dravid.


SCJP 1.4 (progress)
You're not going crazy. You're going sane in a crazy word. Find comfort in this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 813 times.
Similar Threads
static block
can anyone explain output
static initializers
Static Initializer + Variable Declaration
static block variable Vs static variable
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 09:24:57.