Search code examples
javastaticblock

Can I define multiple static blocks?


Can I define multiple static blocks?

If possible, why should I define muliple static blocks?


Solution

  • yes, you can also make multiple initialisation blocks.

    This allows you to place code with the thing initialised.

    private static final Map<String, String> map;
    static {
       // complex code to initialise map
    }
    
    private static final DbConnection conn;
    static {
      // handle any exceptions and initialise conn
    }