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
// 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.