The code for the static initializer is exceeding the 65535 bytes limit error in java?(静态初始化程序的代码超出 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 字节限制错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:静态初始化程序的代码超出 java 中的 65535 字节限制错误?


基础教程推荐
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 降序排序:Java Map 2022-01-01