Final (Java)
Encyclopedia
In the Java programming language
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...

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

Final classes

A final class
Class (computer science)
In object-oriented programming, a class is a construct that is used as a blueprint to create instances of itself – referred to as class instances, class objects, instance objects or simply objects. A class defines constituent members which enable these class instances to have state and behavior...

cannot be extended. 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 {...}


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

Final methods

A final method
Method (computer science)
In object-oriented programming, a method is a subroutine associated with a class. Methods define the behavior to be exhibited by instances of the associated class at program run time...

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.

Example:

public class MyClass {
public final void 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 are loaded at runtime and might not be the same version as the ones that were just compiled. Further, the runtime environment and JIT
Just-in-time compilation
In computing, just-in-time compilation , also known as dynamic translation, is a method to improve the runtime performance of computer programs. Historically, computer programs had two modes of runtime operation, either interpreted or static compilation...

 compiler have the information about exactly which 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 variable
Variable (programming)
In computer programming, a variable is a symbolic name given to some known or unknown quantity or information, for the purpose of allowing the name to be used independently of the information it represents...

can only be initialized once, either via an initializer or an assignment statement. It need not be initialized at the point of declaration: this is called a 'blank final' variable. A blank final instance variable of a class must be definitely assigned at the end of every constructor of the class in which it is declared; similarly, a blank final static variable must be definitely assigned in a static initializer of the class in which it is declared: otherwise, a compile-time error occurs in both cases. (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 double PI = 3.141592653589793; // pi is a universal constant, about as constant as anything can be.

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, zpos > will meet with a compile error. In fact, even if the constructor doesn't set a final variable, attempting to set it outside the constructor will result in a compilation 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
Memoization
In computing, memoization is an optimization technique used primarily to speed up computer programs by having function calls avoid repeating the calculation of results for previously processed inputs...

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.

Blank final

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

In general, a Java compiler will ensure that the blank final is not used until it is assigned a value and that once assigned a value, the now final variable cannot be reassigned another value.

Difference from the C++ "const" type qualifier

A final variable in Java means that that variable can't be reassigned after initialization. C++ const can be used in this way (SomeClass * const ptr), but this is the less common use of const. The more common use of C++ const (const SomeClass * ptr) means that the data being referenced can't be modified, although the reference itself can be changed to refer to different data. Both usages of C++ const can be combined (const SomeClass * const ptr).

Java's final is a hard rule. If you have a final variable, the programmer can not reassign it.

External links

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK