| Author |
When will we go for static block...?
|
Naveen Raja
Greenhorn
Joined: Jul 28, 2006
Posts: 10
|
|
Hi, I know that static block can be defined in a class,but when we have to go for it and how compiler treats it..? Thanks in Advance Naveen.
|
 |
Nikhil Goel
Ranch Hand
Joined: Jul 11, 2003
Posts: 32
|
|
static blocks are intialized the first thing even way before constructors. Usage can be to intialize connection pools etc . Normall all other utility stuff can be taken care in static blocks
|
 |
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
|
|
JDBC driver, for example, use static blocks to register themselves to the DriverManager. That's why loading a JDBC driver by Class.forName initializes it. Another use is to initialize complex static variables, such as Collections etc. They shouldn't be used too often, though - most often there are better (more flexible) alternatives to static blocks.
|
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
|
 |
Sol Mayer-Orn
Ranch Hand
Joined: Nov 13, 2002
Posts: 310
|
|
Just wanted to add that a static block is executed when the class is first loaded & initialized for use. The JVM spec has more accurate definitions about this, but usually you'll notice that a class is loaded & initialized when you first use it - e.g. when you first try to create an instance, or use some static methods/variables. A classic example:
|
 |
Pratibha Malhotra
Ranch Hand
Joined: Dec 21, 2003
Posts: 199
|
|
To add to Naveen Question, what all resolutions happen at compile time and what all are left for run time. can someone pls point me to appropriate resource thanks ahead,
|
~ Pratibha Malhotra<br /> <br />Sun Certified Java Programmer<br />SCEA 1.4 (In Progress)<br />~~~~~~~~~~~~~~~~~~~~~~~~~~~<br />"Many of life's failures are people who did not realize how close they were to success when they gave up!!"
|
 |
 |
|
|
subject: When will we go for static block...?
|
|
|