Final (Java): Difference between revisions

Content deleted Content added
m Removed the dead link. The page doesn't exist anymore.
Final variables: Misleading. Also in wrong section
Line 84:
 
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: <br>
 
<source lang="Java">
for (final SomeObject obj : someList) {
// do something with obj
</source>
 
Since the obj isvariable onlygoes actuallyout assignedof inscope awith singleeach statementiteration of the loop, it is legalactually evenredeclared thougheach iteration, allowing the same token (ie. <code>obj</code>) willto changebe withused eachto iterationrepresent multiple variables.<ref>{{cite web|url=http://www.cs.cmu.edu/~pattis/15-1XX/15-200/lectures/morejava/lecture.html|title=More Java|last=Pattis|first=Richard E.|work=Advanced Programming/Practicum 15–200|publisher=School of Computer Science [[Carnegie Mellon University]]|accessdate=23 July 2010}}</ref>
 
===Final and inner classes===
Line 115 ⟶ 125:
 
The '''blank final''', which was introduced in Java 1.1, is a final variable whose declaration lacks an initializer.<ref>{{cite book|last=Flanagan|first=David |title=Java in a Nutshell|url=http://web.deu.edu.tr/doc/oreily/java/javanut/ch05_06.htm|edition=2nd Edition|date=May 1997 |publisher=O'Reilly|isbn=1-56592-262-X|chapter=Chapter 5 Inner Classes and Other New Language Features:5.6 Other New Features of Java 1.1}}</ref><ref>{{cite web|url=http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html|title=Types, Values, and Variables|year=2000|work=Java Language Specification Chapter 4|publisher=Sun Microsystems, Inc|accessdate=23 July 2010}}</ref> A blank final can only be assigned once and 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 occurs.<ref>{{cite web|url=http://java.sun.com/docs/books/jls/second_edition/html/defAssign.doc.html|title=Definite Assignment|year=2000|work=Java Language Specification|publisher=Sun Microsystems, Inc.|accessdate=23 July 2010}}</ref>
 
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. However it is a misconception to think a final variable can itself not change, you simply cannot assign it more than once. For example, the following is a legal statement: <br>
<source lang="Java">
 
for (final SomeObject obj : someList) {
// do something with obj
</source>
 
Since obj is only actually assigned in a single statement, it is legal even though obj will change with each iteration.<ref>{{cite web|url=http://www.cs.cmu.edu/~pattis/15-1XX/15-200/lectures/morejava/lecture.html|title=More Java|last=Pattis|first=Richard E.|work=Advanced Programming/Practicum 15–200|publisher=School of Computer Science [[Carnegie Mellon University]]|accessdate=23 July 2010}}</ref>
 
==Difference from the C++ <code>const</code> type qualifier==