Static Blocks in Java with Example


Static block in a class are the block of code portion which  executed only once,

1.Before creation of any object of the class.

2.Before any static member of the class in been accessed.

Example: Before creation of any object of the class

package com.devil.java.concept;

public class StaticBlockForConstructor {

static {
System.out.println("I am in first static block");
}

public StaticBlockForConstructor() {
System.out.println("I am in the constructor");
}

public static void main(String args[]) {
StaticBlockForConstructor constr1 = new StaticBlockForConstructor();
StaticBlockForConstructor constr2 = new StaticBlockForConstructor();
}
}

 
Important point to note that static block is been exceuted only once when we create first object constr1
and not when second object constr2 is been created .Output of the above programme is :

I am in first static block
I am in the constructor
I am in the constructor

We can use multiple static blocks as well, they will be executed in the order they are defined in the class, like:

package com.devil.java.concept;

public class StaticBlockForConstructor {

static {
System.out.println("I am in First static block");
}

static {
System.out.println("I am in Second static block");
}

static {
System.out.println("I am in Third static block");
}

public StaticBlockForConstructor() {
System.out.println("I am in the constructor");
}

public static void main(String args[]) {
StaticBlockForConstructor constr1 = new StaticBlockForConstructor();
StaticBlockForConstructor constr2 = new StaticBlockForConstructor();
}
}

Output of the above programme will be :

I am in First static block
I am in Second static block
I am in Third static block
I am in the constructor
I am in the constructor

Example: Before any static member of the class in been accessed.

package com.devil.java.concept;

public class StaticBlock {
static int i;

static {
i = 10;
System.out.println("static block called here");
}

public static void main(String args[]) {

System.out.println(StaticBlock.i);
}
}

Output of the above programme will be:

static block called here
80

If we use multiple variables , in that case also static block will be executed only once like:

package com.devil.java.concept;

public class StaticBlock {
static int i;
static int j;
static int k;

static {
i = 80;
j = 90;
k = 150;
System.out.println("static block called here");
}

public static void main(String args[]) {

System.out.println(StaticBlock.i);
System.out.println(StaticBlock.j);
System.out.println(StaticBlock.k);
}
}

Output of the above programme will be:

static block called here
80
90
150

So that is it about the static block in Java. Hope it will be helpful.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s