February 29, 2008

Quick Java Trivia

Can you guess that what could be the maximum number of methods in a Java Class?

According to JVM Specification:

The number of methods that may be declared by a class or interface is limited to 65535 by the size of the methods_count item of the ClassFile structure (§4.1). Note that the value of the methods_count item of the ClassFile structure does not include methods that are inherited from superclasses or superinterfaces.

4 comments:

  1. nice to know, hope we never see a class with methods_count methods in plain Java :) however for some dynamic JVM languages and some dynamic bytecode processing use cases this limitation might be very relevant.

    ReplyDelete
  2. Actually, Ivan, each and every class you have seen has exactly methods_count number of methods. :P

    Also, which dynamic languages you refer to? I don't see how they could ever hit the limit, in the face of the invokedynamic bytecode. (It would still be hard to believe even without that).

    ReplyDelete
  3. It's also the max number of operations allowed in any one method.

    How do I know? Let's just say I've looked after some fairly awesome apps...

    ReplyDelete
  4. There is an limit for max count of statements per method. Don't know the excat limit any more, but i hit the limit some time ago. I tested a semantic loop-unrolling for a masked-based image-processing operation. The masked was controlling the loop-unrolling. And yes, my unrolling was quicker but i often hit this limit :-(

    ReplyDelete