Home > DeveloperSection > Blogs > The static Keyword: Multiple Static Blocks

The static Keyword: Multiple Static Blocks

Java Java  Software Development 
0 Comment(s)
 137  View(s)
Rate this:

The static Keyword: Multiple Static Blocks


A static block is executed only once when the class is loaded. Our class may contain multiple static blocks. These are executed in the order in which they appear in the class. This can be useful in organizing the multiple logical initializations our class may require in a sequential order. Consider a class that creates a Sudoku puzzle. Creating a puzzle requires certain steps to be executed in sequence. The class definition to implement this may look like the following:

class Puzzle {                

                     static {

                             // Initialize random number generator


                     static {

                             // Formulate Sudoku solution board


                     static {

                             // Make puzzle


                     // Rest of the class code presents the generated puzzle to the user



The three static blocks execute in the sequence they are specified. Thus, first the random number generator will be initialized, followed by the formulation of the Sudoku solution board, and lastly the code that generates puzzle will be executed. When all three blocks execute successfully, a puzzle will be ready for the user.

Another example would be the GoogleConnector class we discussed in the previous post. This class might first initialize a modem, followed by making a Wi-Fi connection and then a network connection to the Google site. These three operations can be arranged in three independent static blocks and called sequentially in the given order in the class definition. Other places where we would use multiple static blocks include loading a database driver, followed by a connection to a database, and loading resource bundles for internationalization in a specific sequence. Be aware that loading expensive resources in a static block may not always be a good idea. What’s more, handling failures in a static block is usually difficult.


An Alternative to a Static Initializer

Instead of using a static block as discussed in the previous post, we can initialize our static fields by defining a private static method. Let’s look at how to do this in our ExtendedBallGame example. The game has a static field called radius. We could initialize this field using the following code snippet:

private static int radius = initClassVariables();


private static int initClassVariables()


       // some computations to determine radius

       int radius = 5;

       return radius;



Defining a method like this for initialization is really useful if we have to reinitialize our objects later in the code. We may then simply call this private class method whenever a re-initialization is desired. For example, in our ball game, when two balls touch each other, we may want to create a bigger ball out of the two balls by merging them into a single ball. Thus, the initialization method gives us more flexibility in coding as compared to a static initialization block.

Few Important Notes on Static Initializers

Here are a few important points on using static blocks to keep in mind while coding these initializers:

·         The JVM sets a limit of 64K on the size of a static initializer block. Therefore, we cannot put lot of code in this block.

·         We cannot throw checked exceptions from a static initializer.

·         We cannot use the this keyword in a static block because no instance has been created so far.

·         We cannot call super explicitly in a static block. Static blocks are loaded when the class is loaded, and super is called whenever object creation takes place. Therefore, it is built into a non-static initializer (that is, a constructor). That’s why including it in a static block results in generating a compile-time error.

·         There is no return type for a static block.

·         Testing the code in a static block is usually considered a nightmare by developers.

Don't want to miss updates? Please click the below button!

Follow MindStick