java final parameter

一些代码里,参数前加了 final 修饰符,不太明白这样做的原因,
用google 搜索: java final "parameter" site:jcp.org
得到这个内容。先存个档,以后仔细看。

http://jcp.org/aboutJava/communityprocess/maintenance/JLS/innerclasses.pdf

Note the final declaration. Local final variables such as array are a new
feature in 1.1. In fact, if a local variable or parameter in one class is referred to
by another (inner) class, it must be declared final. Because of potential
synchronization problems, there is by design no way for two objects to share
access to a changeable local variable. The state variable count could not be
coded as a local variable, unless perhaps it were changed a one-element array:


Enumeration myEnumerate(final Object array[]) {
final int count[] = {0}; // final reference to mutable array
class E implements Enumeration {
public boolean hasMoreElements()
{ return count[0] < array.length; } ...


(Sometimes the combination of inheritance and lexical scoping can be
confusing. For example, if the class E inherited a field named array from
Enumeration, the field would hide the parameter of the same name in the
enclosing scope. To prevent ambiguity in such cases, Java 1.1 allows inherited
names to hide ones defined in enclosing block or class scopes, but prohibits
them from being used without explicit qualification.)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值