如何修复"方法代码..超过65535字节限制"?

The*_*ner 29 java compiler-errors

我有以下代码:

public static void main(String[] args) {
    try {
        String[] studentnames = {
            /* this is an array of 9000 strings... */
        };
    }
}
Run Code Online (Sandbox Code Playgroud)

尝试编译时出现以下错误:

The code of method main(String[]) is exceeding the 65535 bytes limit
Run Code Online (Sandbox Code Playgroud)

Pra*_*mar 26

在java中,方法不能超过65535个字节.

因此,要解决此问题,请将您的main(String[] args)方法分解为多个子方法.


Inf*_*tor 12

错误代码似乎很明显.

The code of method main(String[]) is exceeding the 65535 bytes limit 
Run Code Online (Sandbox Code Playgroud)

这是因为对于方法大小,Java中的任意硬编码限制为64Kb.(实际上许多其他东西仅限于64K,例如方法名称,常量数等.有关更多详细信息,请参阅Java 8规范Java 7规范.)

要解决这个问题,您所要做的就是将main(String[] args)方法分解为多个子方法.


但为什么不直接从文件中加载名称呢?

以您目前提出的方式执行此操作的一些问题包括:

  • 首先,你是硬编码的细节,这几乎总是一件坏事(见这个);

  • 其次,你得到的错误信息; 和

  • 第三,你使你的代码很难阅读.

当然还有更多,但这些是显而易见的.

  • 有几个问题:首先,你是硬编码细节,这几乎总是一件坏事; 其次,你得到的错误信息; 第三,你使你的代码非常难以理解.当然还有更多,但这些是显而易见的. (5认同)