关闭

java中的final

252人阅读 评论(0) 收藏 举报

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)

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:639490次
    • 积分:10337
    • 等级:
    • 排名:第1681名
    • 原创:378篇
    • 转载:267篇
    • 译文:1篇
    • 评论:33条
    文章分类
    最新评论