Why are recursive methods usually static in Java?

2019-03-17 02:11发布

问题:

I'm reviewing recursion these days and found out that a lot of Java recursive method samples are static. Can somebody explain why?

My guess is that they want to save memory, since logically a "parent" recursive method is staying dumb as long as its children recursive method is running and doesn't return anything yet.

Also please check if the following guess is right:

The static recursive method is running on the same thread while a non-static one may run on separate threads, is this correct or doesn't make any sense?

回答1:

Because a recursive method/algorithm is generally controlled solely by its parameters and not by instance variables.

Any method that does not rely on instance variables can (and probably should) be made static.