欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java final parameter

程序员文章站 2022-07-15 13:23:35
...
一些代码里,参数前加了 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.)