方法参数中的final关键字 [英] final keyword in method parameters

查看:111
本文介绍了方法参数中的final关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常会遇到如下所示的方法:

I often encounter methods which look like the following:

public void foo(final String a, final int[] b, final Object1 c){
}

如果在不传递此方法的情况下调用此方法会发生什么最终参数。即稍后更改的Object1(因此未声明为final)可以很好地传递给此方法

What happens if this method is called without passing it final parameters. i.e. an Object1 that is later changed (so is not declared as final) can be passed to this method just fine

推荐答案

Java总是如此在将参数发送给方法之前复制参数。这意味着最终并不意味着调用代码有任何区别。这仅意味着在方法内部无法重新分配变量。 (请注意,如果您有最终对象,您仍然可以更改对象的属性。)

Java always makes a copy of parameters before sending them to methods. This means the final doesn't mean any difference for the calling code. This only means that inside the method the variables can not be reassigned. (note that if you have a final object, you can still change the attributes of the object).

这篇关于方法参数中的final关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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