为什么有人会添加附加的局部变量,只是将final关键字放在上面呢? [英] Why would anyone make additional local variable just to put final keyword on it?

查看:83
本文介绍了为什么有人会添加附加的局部变量,只是将final关键字放在上面呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了类似于以下代码:

public void foo(String param1) { 
    final String param1F = param1;
    ...
}

我怀疑作者不知道他是否可以将final关键字直接放在方法签名中,因为在其余方法中,他仅使用param1F,但我很好奇是否有人对此有什么想法?

解决方案

在这种情况下,您可以重新分配param1,如果param1是最终的,则不可能.

所以有一点区别.但是对我来说,这是没有用的,只是因为我一般不会更改方法参数.

I've encountered the code similar to this:

public void foo(String param1) { 
    final String param1F = param1;
    ...
}

I doubt that the author doesn't know that he can put final keyword directly in the method signature cause in the rest of the method he only uses param1F but I'm curious if anyone has the idea for what this could be useful?

解决方案

In this case, you could reassign param1, which wouldn't be possible, if param1 was final.

So there is a slight difference. But to me it is not useful, just because I do not change method parameters in general.

这篇关于为什么有人会添加附加的局部变量,只是将final关键字放在上面呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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