博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
final (Java)
阅读量:6799 次
发布时间:2019-06-26

本文共 2475 字,大约阅读时间需要 8 分钟。

From Wikipedia, the free encyclopedia

Jump to: ,

In the , the final is used in several different contexts to define an entity which cannot later be changed.

Contents

[]

[] Final classes

A final cannot be . This is done for reasons of security and efficiency. Accordingly, many of the Java standard library classes are final, for example and . All methods in a final class are implicitly final.

Example:

public final class MyFinalClass ...

[] Final methods

A final cannot be by subclasses. This is used to prevent unexpected behavior from a subclass altering a method that may be crucial to the function or consistency of the class.

Example:

public class MyClass     public final  myFinalMethod ...

A common misconception is that declaring a class or method final improves efficiency by allowing the compiler to directly insert the method inline wherever it is called. This is not completely true; the compiler is unable to do this because the classes loaded at runtime might not be the same versions of the ones that were just compiled. Further, the runtime environment and compiler have the information about exactly what classes have been loaded, and are able to make better decisions about when to inline, whether or not the method is final.

[] Final variables

A final is ; it can only be assigned to once. If the variable is a field of a class, it must be assigned to in the constructor of its class. (Note: If the variable is a reference, this means that the variable cannot be re-bound to reference another object. But the object that it references is still mutable, if it was originally mutable.)

Unlike the value of a , the value of a final variable is not necessarily known at compile time.

Example:

public class Sphere      public static final  PI = ;      public final  radius;    public final  xpos;    public final  ypos;    public final  zpos;     Sphere x,  y,  z,  r          radius = r;         xpos = x;         ypos = y;         zpos = z;         ...

Immutability of variables has great advantages, especially in optimization. For instance, Sphere will probably have a function returning its volume; knowing that its radius is constant allows us to the computed volume. If we have relatively few Spheres and we need their volumes very often, the gain might be substantial. Making the radius of a Sphere final informs developers and compilers that this sort of optimization is possible in all code that uses Spheres.

[] External links

Retrieved from " "

转载地址:http://emuwl.baihongyu.com/

你可能感兴趣的文章
inno setup中文支持
查看>>
js内存泄漏的问题?
查看>>
程序代码阅读与分析
查看>>
Linux 安装PHP PECL 百分百成功
查看>>
关于c++风格 code style
查看>>
svn 常用
查看>>
SVM支持向量机
查看>>
Asymptote 学习记录(2):例子阅读
查看>>
《常微分方程教程》习题2-2,4:一个跟踪问题
查看>>
陶哲轩实分析例17.2.3
查看>>
兩個集合之間的全體部分函數可以形成一個集合
查看>>
Elementary Methods in Number Theory Exercise 1.2.17
查看>>
认识拨号计划 - Dialplan
查看>>
DataTable 的数据导出到 Excel
查看>>
委托由浅入深学习
查看>>
BZOJ 1012 [JSOI2008]最大数maxnumber
查看>>
权限管理[Linux]
查看>>
unity3d优化总结篇(二)
查看>>
自定义view,实现文本自动换行
查看>>
查看网页自动保存的密码
查看>>