java中的final

来源:互联网 发布:简单的java反转字符串 编辑:程序博客网 时间:2024/05/19 01:07

The final keyword can be applied to classes, methods, or fields.
When applied to a class, it means that the class cannot be subclassed. 不能被继承
When applied to a method, it means that the method cannot be overridden by a subclass. 不能被重载
When applied to a field, it means that the field's value must be assigned exactly once in each constructor and can never change after that. 但是这里只针对基本类型有效

While an array reference can be declared as final, the elements of the array cannot. This means that classes that expose public final array fields or return references to those fields through their methods.
Similarly, while an object reference may be declared as a final field, the object to which it refers may still be mutable. If you wish to create immutable objects using final fields, you must prevent references to arrays or mutable objects from escaping from your class.

final string can not be changed with replace method(replace method just reture a new string)

原创粉丝点击