Number of methods per Java class

Asked

Viewed 64 times

1

I would like to know if there is a limit amount of methods per java class, if there is how I should proceed in cases where I need to exceed the limit ?

  • 2

    Do you refer in practice, in terms of memory or language limitation, or ideally speaking, as "good practice"? But regardless, if your class starts to have methods in excess, there is something wrong and it would be better to review the concepts of unit code and single responsibility.

  • good practices, I have an interface with a very expressive amount, and I need to add more methods. In this case, I’m afraid to create a rsrs monster.

  • Apparently you’ve created... Does this interface really require so many methods? Are they all required to perform the responsibility of this unit? This shouldn’t be divided into more classes/interfaces?

  • truth, any hint to clean up this mess? maybe re-organize the methods by class!?

  • ** Shouldn’t this be divided into more classes/interfaces? ** I just thought about it

  • For this, only knowing how the class is and what it should do... without the code is impossible to affirm anything concrete. Who knows you can [Dit] the question and switch to that.

  • Woss, this my interface extends Jparepository, so I have several query utility methods. Of any for vc ever helped me, I will move the new methods to a specific interface.

  • 1

    And a lot of it also depends on the application requirements. You will often read in various sources that you should not do such a thing, but in the context of its application can make sense, can solve the problem more simply and would not have why not do. Never blindly follow guidelines; for example, if someone defined that the limit should be 10 methods, it would make no sense to create a new class because you need 11 and they are directly related. It is important to always understand and analyze the situation.

  • There is no "magic" number of methods, it depends on the context. And it’s not just the amount that counts: having 30 small methods that do a single function each and that are directly related (so it makes sense to be in the same class) can be better than 2 giant, confusing methods that do a lot of things (and which could be broken down into smaller methods, or even into other classes). That’s why I don’t like these absolute rules that give magic numbers (like "cannot have more than X methods/lines/parameters"), because the context is more important (besides clarity, cohesion, etc).

  • 1

    Rules like this can even serve as guidelines or general guidelines (if you have "a lot", it is a warning to review, for example), but it is important to analyze the whole, the context, the problem, because there may be situations where it makes more sense to break the rules (as @Woss said, if the rule is X methods, but do total sense create X + 1, it is worth creating another class just for this extra method, in the name of "good practice"? I think not). Good practices should be guides, which you follow only when it makes sense, not absolute laws that you make blindly without analyzing the whole.

Show 5 more comments

1 answer

3

Yes there is a limit that is imposed by the Java JVM (Java Virtual Machine), this limit is 65535 which is the equivalent of 216 - 1, which is the highest number that can be represented by a 16-bit binary number without signal.

I’m not saying that it’s impossible for you to reach that number, I just think it’s unlikely, because you’d probably be running away from good programming practice, but if by some chance you reach that limit you should create a new class and implement those methods in the new class.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.