Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are recursive methods usually static in Java?

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?

like image 952
Sherman_Meow Avatar asked Dec 21 '25 05:12

Sherman_Meow


1 Answers

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.

like image 183
OldCurmudgeon Avatar answered Dec 23 '25 20:12

OldCurmudgeon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!