Execution order of of static blocks in an Enum type w.r.t to constructor

前端 未结 3 738
栀梦
栀梦 2020-12-19 00:01

This is from Effective Java :

// Implementing a fromString method on an enum type
  private static final Map stringToEnum
      = ne         


        
3条回答
  •  温柔的废话
    2020-12-19 00:26

    Operation constants are static fields created in the static block in the appearing order.

    static { 
        // instantiate enum instances here
        ...
        // Initialize map from constant name to enum constant     
        for (Operation op : values())       
           stringToEnum.put(op.toString(), op);   
    } 
    

提交回复
热议问题