Maximum size of a method in java?

后端 未结 3 580
感动是毒
感动是毒 2020-11-27 19:30

I come to know that the maximum size of a method in java is 64k. And if it exceeds, we\'ll get a compiler warning like \"Code too large to compile\". So can we call this a

相关标签:
3条回答
  • 2020-11-27 19:51

    If your method is longer than 50 lines including inside comments - split it. In this case you will never reach any limitation (even if one exists).

    I personally saw 1000 lines long methods (written by criminals that call themselves programmers :) ) but did not see such kind of limitation.

    0 讨论(0)
  • 2020-11-27 19:58

    64k is quite a lot, if you exceed it you may think about reorganizing you code.

    In my project I met this constraint once in generated sources. Solved by just splitting one method to several.

    0 讨论(0)
  • 2020-11-27 20:08

    In my experience the 64KB limit is only a problem for generated code. esp. when intiialising large arrays (which is done in code)

    In well structured code, each method is a manageable length and is much smaller than this limit. Large pieces of data, to be loaded into arrays, can be read from a non Java files like a text or binary file.

    EDIT:

    It is worth nothing that the JIT won't compile methods larger than 8 K. This means the code runs slower and can impact the GC times (as it is less efficient to search the call stack of a thread with methods which are not compiled esp big ones)

    If possible you want to limit your methods to 8 K rather than 64 K.

    0 讨论(0)
提交回复
热议问题