Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maximum lines of code permitted in a Java class?

Tags:

java

How many lines of code can a .java file contain? Does it depend on the JVM being used?

like image 247
kiritsinh parmar Avatar asked Sep 20 '08 10:09

kiritsinh parmar


People also ask

How many lines of code is too much for a class?

a) Methods should not have more than an average of 30 code lines (not counting line spaces and comments). b) A class should contain an average of less than 30 methods, resulting in up to 900 lines of code. c) A package shouldn't contain more than 30 classes, thus comprising up to 27,000 code lines.

Is there a limit to lines of code?

There is no "global limit", but you could have an IDE large limit. Anyway, it's a good practice to have a limit because the code has to be readable ( E.g.: 80-120 characters ).

How big can Java classes be?

But a class can have 65535 attributes, plus 65535 fields, each of them having 65535 attributes of its own and plus 65535 methods, each of them having up to 65535 attribute as well.

How many lines minimum lines of code are needed in Java?

This is the smallest code if you want to display something so that the user may know that the program have worked proprely and given some output and its of almost 4 lines. its a code which will be compiled by the compiler and it will not return any error.


2 Answers

To extend upon Jonas's response, the Java Virtual Machine Specification, Section 4.8 Constraints on Java Virtual Machine Code says that:

The Java virtual machine code for a method, instance initialization method (§3.9), or class or interface initialization method (§3.9) is stored in the code array of the Code attribute of a method_info structure of a class file. This section describes the constraints associated with the contents of the Code_attribute structure.

Continuing to Section 4.8.1, Static Constraints

The static constraints on a class file are those defining the well-formedness of the file. With the exception of the static constraints on the Java virtual machine code of the class file, these constraints have been given in the previous section. The static constraints on the Java virtual machine code in a class file specify how Java virtual machine instructions must be laid out in the code array and what the operands of individual instructions must be.

The static constraints on the instructions in the code array are as follows:

...

  • The value of the code_length item must be less than 65536.

...

So a method does have a limit of 65535 bytes of bytecode per method. (see note below)

For more limitations to the JVM, see Section 4.10 Limitations of the Java Virtual Machine.

Note: Although there is apparently a problem with the design of the JVM, where if the instruction at byte 65535 is an instruction that is 1 byte long, it is not protected by exception handler - this is listed in footnote 4 of Section 4.10.

like image 132
coobird Avatar answered Sep 24 '22 06:09

coobird


I believe there is a 64kb limit on bytecode size per method.

like image 26
Jonas Klemming Avatar answered Sep 22 '22 06:09

Jonas Klemming