静态初始化程序的代码超出 java 中的 65535 字节限制错误? [英] The code for the static initializer is exceeding the 65535 bytes limit error in java?

查看:28
本文介绍了静态初始化程序的代码超出 java 中的 65535 字节限制错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在尝试初始化 4 个长度为 10,100,1000,10000 的字符串数组,这些数组就像

Hi I am trying to initialize 4 string arrays of length 10,100,1000,10000 and these arrays are like

array1={"0","1",..."9"} 
array2={"00","01",..."99"} 
array3={"000","001",..."999"} 
array4={"0000","0001",..."9999"} 

但我得到了 静态初始化程序的代码超出了 65535 字节限制

如何初始化我的数组?

另请注意,从文件中加载它不是我的选择:(

Also note that loading it from file is not an option for me :(

推荐答案

通过从常量池中加载每个值并将其分配给相应的数组索引,在 java 字节码中初始化常量数组.每个数组元素需要几个字节的代码.jvm 方法的大小限制为 65535 字节,因为它的长度使用 16 位数字存储在类文件中.

Constant array in are initialized in java bytecode by loading each value from the constant pool and assigning it to the corresponding array index. This takes several bytes of code per array element. The size of a jvm method is limited to 65535 bytes since its length is stored in the class file using a 16 bit number.

在无法在循环中轻松计算值的情况下,您可以将初始化分解为单独的静态函数:

In the case where the values can not be easily calculated in a loop, you could break the initialization into separate static functions:

static {
    array1 = getValuesForArray1();
    ...
}

private static String[] getValuesForArray1() {
    ...
}

如果初始化值存在模式,那么动态计算值当然更好.

If there is a pattern to the initialization values its of course better to calculate the values on the fly.

这篇关于静态初始化程序的代码超出 java 中的 65535 字节限制错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆