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

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

问题描述

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

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.

在值不容易的情况下以lo计算op,你可以将初始化分解为单独的静态函数:

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天全站免登陆