The maximum number of methods in a class

The other day I saw a Java interface with well over 300 methods (with no inheritance). It’s one of these interfaces that is a single point of entry into a system, and I’m partly to blame. Really didn’t think about how the API should scale, and now that there’s years and years of development depending on this interface, refactoring this single interface is not a trivial class (it would have to be broken up at least in a dozen smaller interfaces).
That got me wondering, what could be the maximum number of methods in a class. A quick google search yielded a forum discussion with the answer as defined in the JVM spec (2nd edition);
The number of methods that may be declared by a class or interface is limited to 65535 by the size of the
methods_countitem of theClassFilestructure (§4.1). Note that the value of themethods_countitem of theClassFilestructure does not include methods that are inherited from superclasses or superinterfaces.
So even if you have 300 methods you are still very far away from ever hitting the limit. I wonder if there are any open source projects out there with classes with this number of methods. If you count inheritance, some of the AWT/Swing classes do have a large amount of methods, but there have to be more examples of this.
Anyways, my little piece of advise is to be forward thinking when designing APIs and make sure the API can scale as more functionality is added. It is always a good idea to think very modular and to group functionality in classes and packages that make sense.
Obviously, when others depend on your code, it never hurts to practice good design usability.
Filed under: Java, Software Development on February 28th, 2008

Leave a Reply