What is difference between static and init block in java


The static block is only loaded when the class object is created by the JVM for the 1st time whereas init {} block is loaded every time class object is created. Also first the static block is loaded then the init block.

public class LoadingBlocks {

static{
System.out.println("Inside static");
}

{
System.out.println("Inside init");
}
public static void main(String args[]){
new LoadingBlocks();
new LoadingBlocks();
new LoadingBlocks();
}
}

Output:

Inside static
Inside init
Inside init
Inside init

Advertisements

11 thoughts on “What is difference between static and init block in java

    1. Dear Belle :I don’t think that we have a VB implemenation of Apriori Algorithm on this site and I do not know of any other site that may have one.But I do know that the comumnity at has a popular java implementation of Apriori with full source code.Please let me know if I can be of more service to you.Thanks Was this answer helpful?

    2. Static block get called only once at very first when the class is loaded by the JVM, It will be called even if no object of this class is created but init block is called every time u create an object of the class, init is like constructor calling be u can not pass parameters to constructor.

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