打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
final (Java)

final (Java)

From Wikipedia, the free encyclopedia

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 classcannot be extended. This is done for reasons of security andefficiency. Accordingly, many of the Java standard library classes arefinal, for example java.lang.System and java.lang.String. All methods in a final class are implicitly final.

Example:

public final class MyFinalClass {...}

Restricted subclasses are often referred to as "soft final" classes.[1]

[edit] Final methods

A final method cannot be overriddenby subclasses. This is used to prevent unexpected behavior from asubclass altering a method that may be crucial to the function orconsistency of the class.[2]

Example:

public class MyClass {
public final void myFinalMethod() {...}
}

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

[edit] Final variables

A final variable can only be assigned once. This assignment does not grant the variable immutablestatus. If the variable is a field of a class, it must be assigned inthe constructor of its class. (Note: If the variable is a reference,this means that the variable cannot be re-bound to reference anotherobject. But the object that it references is still mutable, if it wasoriginally 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 double PI = 3.141592653589793; // this is essentially a constant

public final double radius;
public final double xpos;
public final double ypos;
public final double zpos;

Sphere(double x, double y, double z, double r) {
radius = r;
xpos = x;
ypos = y;
zpos = z;
}

[...]
}

Any attempt to reassign radius, xpos, ypos, zposwill meet with a compile error. In fact, even if the constructordoesn't set a final variable, attempting to set it outside theconstructor will result in a compile error.

To illustrate that finality doesn't guarantee immutability: suppose we replace the three position variables with a single one:

    public final Position pos;

where pos is an object with three properties pos.x, pos.y and pos.z. Then pos cannot be assigned to, but the three properties can, unless they are final themselves.

Like full immutability, finality 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 performance 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] Blank final

The blank final, which was introduced in Java 1.1, is a final variable whose declaration lacks an initializer. [4][5]A blank final can only be assigned once and it must be unassigned whenan assignment occurs. In order to do this, a Java compiler runs a flowanalysis to ensure that, for every assignment to a blank final variable,the variable is definitely unassigned before the assignment; otherwise acompile-time error must occur.[6]

In general, a Java compiler will ensure that the blank final is notused until it is assigned a value and that once assigned a value, thenow final variable cannot be reassigned another value.[7]

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Java多态的实现
Java 将字符串动态生成字节码的实现方法
Why make your classes final? | Andrzej's C++ blog
[大赞]The final local variable xxx cannot be assigned, since it is defined in an enclosing type
java中不可以被继承的类
public <X> List<X> find(final String hql, final Object... values) 第一个<X>是什么意思?
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服