“Code too large” compilation error in Java

2019-01-01 00:51发布

Is there any maximum size for code in Java? I wrote a function with more than 10,000 lines. Actually, each line assigns a value to an array variable.

        arts_bag[10792]="newyorkartworld";
        arts_bag[10793]="leningradschool";
        arts_bag[10794]="mailart";
        arts_bag[10795]="artspan";
        arts_bag[10796]="watercolor";
        arts_bag[10797]="sculptures";
        arts_bag[10798]="stonesculpture";  

And while compiling, I get this error: code too large

How do I overcome this?

9条回答
余生无你
2楼-- · 2019-01-01 01:47

This seems a bit like madness. Can you not initialize the array by reading the values from a text file, or some other data source?

查看更多
余生请多指教
3楼-- · 2019-01-01 01:51

Try to refactor your code. There is limit on the size of method in Java.

查看更多
浅入江南
4楼-- · 2019-01-01 01:52

This error sometimes occur due to too large code in a single function... To solve that error, split that function in multiple functions, like

//Too large code function
private void mySingleFunction(){
.
.
2000 lines of code
}
//To solve the problem
private void mySingleFunction_1(){
.
.
500 lines of code
}
private void mySingleFunction_2(){
.
.
500 lines of code
}
private void mySingleFunction_3(){
.
.
500 lines of code
}
private void mySingleFunction_4(){
.
.
500 lines of code
}
private void MySingleFunction(){
mySingleFunction_1();
mySingleFunction_2();
mySingleFunction_3();
mySingleFunction_4();
}
查看更多
登录 后发表回答