Final (Java)

Final (Java)

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

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 Javadoc:SE|package=java. _ja. System and Javadoc:SE|package=java. _ja. String. All methods in a final class are implicitly final.

Example:public final class MyFinalClass {...}

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. [http://java.sun.com/docs/books/tutorial/java/IandI/final.html]

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 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 [http://www.ibm.com/developerworks/java/library/j-jtp1029.html] .

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 double PI = 3.141592653589793; // this might as well be 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; }

[...] }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.

External links

* [http://javapapers.com/java-interview-questions/explain-the-final-keyword-in-java/ Discussion about a controversy on final keyword]


Wikimedia Foundation. 2010.

Игры ⚽ Поможем сделать НИР

Look at other dictionaries:

  • Final (Java) — En langage Java, le mot clé final indique qu un élément ne peut être changé dans la suite du programme. Il peut s appliquer aux méthodes et attributs d une classe ainsi que la classe elle même. Selon le contexte, on utilise final dans un souci de …   Wikipédia en Français

  • Final — A final may refer to: * the condition of finality *final (competition), the last round of a match, game, or etc. contest, which decides which individual or team will be champion *final examination, a college or university course s final in a… …   Wikipedia

  • Java Platform, Enterprise Edition — Java Platform, Enterprise Edition, abgekürzt Java EE oder früher J2EE, ist die Spezifikation einer Softwarearchitektur für die transaktionsbasierte Ausführung von in Java programmierten Anwendungen und insbesondere Web Anwendungen. Sie ist eine… …   Deutsch Wikipedia

  • Java 2 Enterprise Edition — Java Platform, Enterprise Edition, abgekürzt Java EE oder früher J2EE, ist die Spezifikation einer Softwarearchitektur für die transaktionsbasierte Ausführung von in Java programmierten Anwendungen und insbesondere Web Anwendungen. Sie ist eine… …   Deutsch Wikipedia

  • Java 2 Platform Enterprise Edition — Java Platform, Enterprise Edition, abgekürzt Java EE oder früher J2EE, ist die Spezifikation einer Softwarearchitektur für die transaktionsbasierte Ausführung von in Java programmierten Anwendungen und insbesondere Web Anwendungen. Sie ist eine… …   Deutsch Wikipedia

  • Java EE — Java Platform, Enterprise Edition, abgekürzt Java EE oder früher J2EE, ist die Spezifikation einer Softwarearchitektur für die transaktionsbasierte Ausführung von in Java programmierten Anwendungen und insbesondere Web Anwendungen. Sie ist eine… …   Deutsch Wikipedia

  • Java Platform Enterprise Edition — Java Platform, Enterprise Edition, abgekürzt Java EE oder früher J2EE, ist die Spezifikation einer Softwarearchitektur für die transaktionsbasierte Ausführung von in Java programmierten Anwendungen und insbesondere Web Anwendungen. Sie ist eine… …   Deutsch Wikipedia

  • Java OpenGL — (JSR 231) Скриншот, демонстрирующий …   Википедия

  • Java (Framework) — Java Objektorientierte Programmiersprache Basisdaten Paradigmen: Objektorientierte Programmiersprache Aktuelle  …   Deutsch Wikipedia

  • Java (Programmiersprache) — Java Basisdaten Paradigmen: Objektorientierte Programmiersprache Erscheinungsjahr …   Deutsch Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”