• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

static initializer block

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!
 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
}
}
}
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 123
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This might help
 
Visha Venkat
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
reply
    Bookmark Topic Watch Topic
  • New Topic