View RSS Feed

Java Basic

Static initialize

Rating: 1 votes, 1.00 average.
by , 01-08-2012 at 10:50 AM (1020 Views)
All the code blocks that do not have a function name and are declared as static are executed before the constructor is called. They are usually used to initialize static fields as shown in below code.

Java Code: This is an example of static initialize
public class StaticInitilaizer {
public static final int A = 5;
public static final int B;
//Static initializer block, which is executed only once when the class is loaded.
static {
if(A == 5)
B = 10;
B = 5;
public StaticInitilaizer(){} // constructor is called only after static initializer block
The following code gives an Output of A=5, B=10.

Submit "Static initialize" to Facebook Submit "Static initialize" to Digg Submit "Static initialize" to Submit "Static initialize" to StumbleUpon Submit "Static initialize" to Google

Updated 01-14-2012 at 06:37 PM by Java Basic