Content deleted Content added
mNo edit summary |
mNo edit summary |
||
Line 1:
In the [[Java (programming language)|Java programming language]], the <code>'''final'''</code> [[Keyword (computer)|keyword]] is used in several different contexts to define an entity which cannot later be changed.
= Final classes =
A '''final [[Class (computer science)|class]]''' cannot be [[Subclass (computer science)|subclassed]]. This is done for reasons of security or efficiency. Accordingly, many of the Java standard library classes are final, for example {{Javadoc:SE|package=java.lang|java/lang|System}} and {{Javadoc:SE|package=java.lang|java/lang|String}}. All methods in a final class are implicitly final.
Line 8 ⟶ 10:
public final class MyFinalClass {...}
= Final methods =
A '''final [[Method (computer science)|method]]''' cannot be [[Method overriding (programming)|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]
Line 19 ⟶ 23:
A common misconception is that declaring a class or method final improves efficiency by allowing the compiler to directly insert the method inline where ever it is called. This is not completely true; the compiler is unable to do this because the classes loaded at runtime might not be 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, in the constructor of its class.
|