Final (Java): Difference between revisions

Content deleted Content added
the keyword "final" is the only applied to instance variables (fields) whereas the linked article talks about final as applied to classes and methods. both are unrelated except for being the same word, therefore the link is not related to this entry.
Line 126:
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>
 
==C++ analog==
==Difference from the C++ <code>const</code> type qualifier==
{{details|const (computer programming)}}
In [[C (programming language)|C]] and [[C++]], the analogous construct is the <code>[[const (computer programming)|const]]</code> [[keyword (computer programming)|keyword]]. This differs substantially from <code>final</code> in Java, most basically in being a [[type qualifier]]: <code>const</code> is part of the ''[[data type|type]],'' not only part of the identifier (variable). This also means that the constancy of a value can be changed by casting (explicit type conversion), in this case known as "const casting".
 
AnotherFurther, because C and C++ parallelexpose 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 wherethat the contents being referenced can be modified, but the reference itself cannot (without casting). ThisBy iscontrast, differentwhen applying const fromto the C++referenced data only, as in <code>const SomeClass * ptr</code> where, the contents cannot be modified (without casting), but the reference itself can.
The [[C++]] equivalent would be a reference that cannot be changed after initialization, though the data it points to may be.
 
Due to casting, C++ const 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 breaks or bypasses the final restrictions.
Another C++ parallel is <code>SomeClass * const ptr</code> where the contents being referenced can be modified, but the reference itself cannot without casting. This is different from the C++ <code>const SomeClass * ptr</code> where the contents cannot be modified without casting, but the reference itself can.
 
C++ const 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 breaks or bypasses the final restrictions.
 
==References==