Static Block in Java

JavaStatic

Java Problem Overview


I was looking over some code the other day and I came across:

static {
    ...
}

Coming from C++, I had no idea why that was there. Its not an error because the code compiled fine. What is this "static" block of code?

Java Solutions


Solution 1 - Java

It's a static initializer. It's executed when the class is loaded (or initialized, to be precise, but you usually don't notice the difference).

It can be thought of as a "class constructor".

Note that there are also instance initializers, which look the same, except that they don't have the static keyword. Those are run in addition to the code in the constructor when a new instance of the object is created.

Solution 2 - Java

It is a static initializer. It's executed when the class is loaded and a good place to put initialization of static variables.

From http://java.sun.com/docs/books/tutorial/java/javaOO/initial.html

> A class can have any number of static initialization blocks, and they can appear anywhere in the class body. The runtime system guarantees that static initialization blocks are called in the order that they appear in the source code.

If you have a class with a static look-up map it could look like this

class MyClass {
    static Map<Double, String> labels;
    static {
        labels = new HashMap<Double, String>();
        labels.put(5.5, "five and a half");
        labels.put(7.1, "seven point 1");
    }
    //...
}

It's useful since the above static field could not have been initialized using labels = .... It needs to call the put-method somehow.

Solution 3 - Java

It's a block of code which is executed when the class gets loaded by a classloader. It is meant to do initialization of static members of the class.

It is also possible to write non-static initializers, which look even stranger:

public class Foo {
    {
        // This code will be executed before every constructor
        // but after the call to super()
    }
    
    Foo() {

    }
}

Solution 4 - Java

Static block can be used to show that a program can run without main function also.

//static block
//static block is used to initlize static data member of the clas at the time of clas loading
//static block is exeuted before the main
class B
{
	static
	{
		System.out.println("Welcome to Java"); 
		System.exit(0);	
	}
}

Solution 5 - Java

A static block executes once in the life cycle of any program, another property of static block is that it executes before the main method.

Solution 6 - Java

Static blocks are used for initializaing the code and will be executed when JVM loads the class.Refer to the below link which gives the detailed explanation. http://www.jusfortechies.com/java/core-java/static-blocks.php

Solution 7 - Java

yes, static block is used for initialize the code and it will load at the time JVM start for execution.

static block is used in previous versions of java but in latest version it doesn't work.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMohit DeshpandeView Question on Stackoverflow
Solution 1 - JavaJoachim SauerView Answer on Stackoverflow
Solution 2 - JavaaioobeView Answer on Stackoverflow
Solution 3 - JavaSimon LehmannView Answer on Stackoverflow
Solution 4 - Javauser3777803View Answer on Stackoverflow
Solution 5 - JavaZahid HussainView Answer on Stackoverflow
Solution 6 - JavaSGKView Answer on Stackoverflow
Solution 7 - JavaPiyushView Answer on Stackoverflow