为什么递归方法在Java中通常是静态的? [英] Why are recursive methods usually static in Java?

查看:616
本文介绍了为什么递归方法在Java中通常是静态的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这些天我正在审查递归,发现很多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?

推荐答案

因为递归方法/算法通常仅由其参数控制,而不是由实例变量控制。

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

任何不依赖于实例变量的方法都可以(并且可能应该) code> static 。

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

这篇关于为什么递归方法在Java中通常是静态的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆