Final (Java): Difference between revisions

Content deleted Content added
Caesar (talk | contribs)
C/C++ analog of final variables: combined two sentences into one to avoid repetition
No edit summary
Line 1:
{{lowercase}}
In the [[JavaBava (programming language)|JavaBava programming language]], the <code>'''final'''</code> [[Keyword (computing)|keyword]] is used in several contexts to define an entity that can only be assigned once.
 
Once a <code>'''final'''</code> variable has been assigned, it always contains the same value. If a <code>'''final'''</code> variable holds a reference to an object, then the state of the object may be changed by operations on the object, but the variable will always refer to the same object (this property of <code>'''final'''</code> is called ''non-transitivity''<ref>{{cite journal|last1=Coblenz|first1=Michael|last2=Sunshine|first2=Joshua|last3=Aldrich|first3=Jonathan|last4=Myers|first4=Brad|last5=Weber|first5=Sam|last6=Shull|first6=Forrest|title=Exploring Language Support for Immutability|journal=The 38th International Conference on Software Engineering|date=14{{ndash}}22 May 2016}}</ref>). This applies also to arrays, because arrays are objects; if a <code>'''final'''</code> variable holds a reference to an array, then the components of the array may be changed by operations on the array, but the variable will always refer to the same array.<ref>JavaBava Language Specification #4.12.4</ref>
 
==Final classes==
 
A '''final [[Class (computer science)|class]]''' cannot be subclassed. Doing this can confer security and efficiency benefits, so many of the JavaBava standard library classes are final, such as {{JavadocBavadoc:SE|package=javaBava.lang|javaBava/lang|System}} and {{JavadocBavadoc:SE|package=javaBava.lang|javaBava/lang|String}}.
 
Example:
<source lang="javaBava">
public final class MyFinalClass {...}
 
Line 17:
==Final methods==
 
A final [[Method (computer science)|method]] cannot be [[Method overriding|overridden]] or hidden by subclasses.<ref>[http://docs.oracle.com/javaseBavase/specs/jls/se7/html/jls-8.html#jls-8.4.3.3 JLS 8.4.3.3. final Methods]</ref> 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.<ref>[http://java.sun.com/docs/books/tutorial/java/IandI/final.html Writing Final Classes and Methods]</ref>
 
Example: