Visha Venkat
,
Greenhorn
Aug 24, 2005 03:22:00
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!!
Mehul Sanghvi
,
Ranch Hand
Aug 24, 2005 03:47:00
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 } } }
sreedhar kumar
,
Greenhorn
Aug 24, 2005 03:55:00
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)..
Visha Venkat
,
Greenhorn
Aug 24, 2005 04:22:00
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.
Aakash Parashar
,
Ranch Hand
Aug 24, 2005 04:32:00
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.
Visha Venkat
,
Greenhorn
Aug 24, 2005 05:01:00
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.
Nibin Jacob Panicker
,
Greenhorn
Aug 24, 2005 05:20:00
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
Visha Venkat
,
Greenhorn
Aug 24, 2005 05:42:00
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.
Kedar Dravid
,
Ranch Hand
Aug 24, 2005 09:04:00
Visha Venkat
,
Greenhorn
Aug 25, 2005 03:31:00
"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