Search code examples
javastatic-block

Advantages of multiple static blocks


class A{
    static{
      //initialize all things here
    }
}

This is the way I use static blocks in my code. But as we can also keep multiple static blocks in a class

class A{
   static{
      //do something here
   }

   static{
      //do something else here
   }
}

I have seen cases where multiple static blocks have been used, but can't seem to figure out why?

I suppose if it is for readability below method can also be used

class A{
    static{
       someMethod();
       someOtherMethod();
    }
}

Are there any other advantages of multiple static blocks other than readability?


Solution

  • In your case second one is more readable,as you said. static blocks are executed by the order they placed.In your case there is no other benefits /performance issues.