File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static initializer block

 
Visha Venkat
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 134
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 123
Hibernate Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 333
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This might help
 
Visha Venkat
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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)
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic