Content deleted Content added
m →Final methods: No need for that paragraph to be bold. |
JoeSperrazza (talk | contribs) m clean up, typo(s) fixed: ie. → i.e. using AWB |
||
Line 1:
{{lowercase}}
In the [[Java (programming language)|Java programming language]], the <code>'''final'''</code> [[Keyword (computing)|keyword]] is used in several different contexts to define an entity which may 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 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>Java Language Specification #4.12.4</ref>
Line 87:
Like full [[immutability]], the use of final variables has great advantages, especially in optimization. For instance, <code>Sphere</code> will probably have a function returning its volume; knowing that its radius is constant allows us to [[Memoization|memoize]] the computed volume. If we have relatively few <code>Sphere</code>s and we need their volumes very often, the performance gain might be substantial. Making the radius of a <code>Sphere</code> <code>final</code> informs developers and compilers that this sort of optimization is possible in all code that uses <code>Sphere</code>s.
Though it appears to violate the <code>final</code> principle, the following is a legal statement:
<source lang="Java">
Line 95:
</source>
Since the obj variable goes out of scope with each iteration of the loop, it is actually redeclared each iteration, allowing the same token (
===Final and inner classes===
Line 134:
Further, because C and C++ expose pointers and references directly, there is a distinction between whether the pointer itself is constant, and whether the data pointed to by the pointer is constant. Applying <code>const</code> to a pointer itself, as in <code>SomeClass * const ptr</code>, means that the contents being referenced can be modified, but the reference itself cannot (without casting). This usage results in behaviour which mimics the behaviour of a <code>final</code> variable reference in Java. By contrast, when applying const to the referenced data only, as in <code>const SomeClass * ptr</code>, the contents cannot be modified (without casting), but the reference itself can. Both the reference and the contents being referenced can be declared as <code>const</code>.
Due to casting, C++ <code>const</code> is a soft guideline and it can easily be overridden by the programmer; the programmer can easily cast a const reference to an non-const reference. Java's final is a strict rule such that it is impossible to compile code that directly breaks or bypasses the final restrictions. Using [[
==References==
|