On this section we will be dealing with static block. Before going to the example code, let’s describe first what is static block. A static block is basically a normal block of code that is enclosed with braces and it is preceded with keyword static. Below is the format

static {
	// initialize variables here

Quite easy isn’t it? You might be wondering why we have to go into trouble in using static block? This might be handy in initializing variables. This code is one of the first to get loaded when the class is called. Actually the simple initialization of variables can be done using static variable initialization however this is to simplistic. What if the initialization of class variables requires some complex code logic? The static block suits it best.

Static Block Example

package com.javatutorialhq.java.staticexamples;

public class SomeClass {

	static String firstName = "Albert";
	static String lastName = "Einstein";
	static String fullName;
	static {
		//initialize class variable here
		fullName = firstName + " " + lastName;
	public static void main(String[] args) {		



Sample Output

If you run the above example, we would be having the following output

Albert Einstein