Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Java limit the size of a method to 65535 byte?

Tags:

java

I just compiled the following code

public class A {
    public static void main(String... args) {
        int i = 3;
        ++i; 
        ++i;
        ++i;
        ++i;
        ++i;
        ++i;
        ++i;
        ++i;
        // repeat writing the expression ++i for 20,000 times

        System.out.println(i);
    }
}

And got the following error message

The code of method main(String...) is exceeding the 65535 bytes limit

Why does Java implement this limit? I don't see the rational since Java does include a goto_w instruction.

like image 462
Konstantin Weitz Avatar asked Apr 16 '11 21:04

Konstantin Weitz


People also ask

Is exceeding the 65535 bytes limit Java?

Re: exceeding the 65535 bytes limit Wikipedia: A Java class or interface can have at most 65535 methods. The code of a constructor in Java is limited to 65535 bytes.


1 Answers

See the Java Virtual Machine Specification section 4.10:

4.10 Limitations of the Java Virtual Machine

  • The amount of code per non-native, non-abstract method is limited to 65536 bytes by the sizes of the indices in the exception_table of the Code attribute (§4.7.3), in the LineNumberTable attribute (§4.7.8), and in the LocalVariableTable attribute (§4.7.9).

There's few good reasons to have a method that long in an object-oriented programming language.

like image 154
WhiteFang34 Avatar answered Oct 12 '22 23:10

WhiteFang34