方法签名中包含最终输入参数意味着什么?
执行以下操作的原因是什么:
public void processSomething(final String hello, final String two, final Car car){}
而不是:
public void processSomething(String hello, String two, Car car){}
What would be the reason for doing the below:
public void processSomething(final String hello, final String two, final Car car){}
as opposed to:
public void processSomething(String hello, String two, Car car){}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这意味着在方法内,您无法为参数分配新值。
想要这样做的一个常见原因是能够使用 中的参数匿名内部类只能引用
final
局部变量,包括参数。这样做的另一个原因是,如果可能的话,您的编码风格倾向于将所有局部变量声明为final。 (我个人尝试将它们视为最终的,但实际上避免以这种方式声明它们,因为这会增加麻烦。)
It means that within the method, you can't assign new values to the parameters.
A common reason for wanting to do this is to be able to use the parameters within anonymous inner classes which can only reference
final
local variables, including parameters.Another reason for doing this is if your coding style favours declaring all local variables as final if possible. (Personally I try to treat them as final, but avoid actually declaring them that way, as it adds cruft.)
这意味着您无法更改引用。字符串是不可变的,但如果汽车是可变的,您可以更改该汽车中的字段,但不能将其更改为另一辆汽车。
It means you cannot change the references. String is immutable, but if Car is mutable you can change the fields in that Car, you can't change it to another Car.
这意味着您无法更改引用,但它无法阻止对象被更改。
It means you cannot change the references, but it cannot stop the object from being altered.
这意味着该对象的引用不能在方法内更改。
It means that the reference of this Object can not be changed within the method.