Does an Enum Class containing 2000+1 Enum Constants hit any limit?

后端 未结 2 1385

The following code fails with a NullPointerException in main (map==null). The issue occurs only if I define 2001 or more Enum constants, 2000 work fine

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-02 00:31

    I suspect what you should be seeing is an error when you compile

    error: code too large
    

    Perhaps your version of the compiler has a bug and doesn't show this.

    When I create 2500 enums values it fails with this error but with 2400 enum values it runs correctly.

    There is a limit of 64 KB for the byte code of any method and the enums are initialised in the one method for the static initializer block.

    The problem is that many byte code instructions use the byte offset as a 16-bit value which places this limitation on the whole method (even if there is no such instruction at the end of a method)

    The javac is not smart enough to break up the static initialiser block into multiple sub-methods, but then again having thousands of enums suggest you could do what is required another way.

提交回复
热议问题