aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes static initializer block Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "static initializer block" Watch "static initializer block" New topic
Author

static initializer block

Visha Venkat
Greenhorn

Joined: Aug 18, 2005
Posts: 11
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

Joined: Feb 04, 2002
Posts: 134
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

Joined: Aug 24, 2005
Posts: 3
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

Joined: Aug 18, 2005
Posts: 11
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

Joined: Jul 25, 2005
Posts: 123

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.


Your's Aakash
SCJP 1.4 96%, SCBCD 1.3 94%, http://java-application-programming.blogspot.in
Visha Venkat
Greenhorn

Joined: Aug 18, 2005
Posts: 11
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

Joined: Aug 16, 2005
Posts: 22
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

Joined: Aug 18, 2005
Posts: 11
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

Joined: May 28, 2004
Posts: 333
This might help
Visha Venkat
Greenhorn

Joined: Aug 18, 2005
Posts: 11
"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)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: static initializer block