Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The code for the static initializer is exceeding the 65535 bytes limit error in java?

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"} 

But I am getting the error of The code for the static initializer is exceeding the 65535 bytes limit

how can I initialize my arrays?

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

like image 568
waqas Avatar asked Dec 04 '22 16:12

waqas


1 Answers

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.

like image 191
Jörn Horstmann Avatar answered Jan 19 '23 01:01

Jörn Horstmann