final (Java)

From Wikipedia, the free encyclopedia

Jump to: navigation, search

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


Contents

[hide]

[edit] Final classes

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

Example:

public final class MyFinalClass ...

[edit] Final methods

A final method cannot be overridden 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.[1]

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 JIT 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[2].

[edit] Final variables

A final variable is immutable; 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 constant, 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 memoize 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.

[edit] External links

转载于:https://www.cnblogs.com/licheng/archive/2008/08/29/1279214.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值