Content deleted Content added
grammar |
Tuankiet65 (talk | contribs) mNo edit summary |
||
Line 5:
==Final classes==
A '''final [[Class (computer science)|class]]''' cannot be subclassed. As doing this can confer security and efficiency benefits, many of the Java standard library classes are final, such as {{Javadoc:SE|package=java.lang|java/lang|System}} and {{Javadoc:SE|package=java.lang|java/lang|String}}.
Line 45 ⟶ 44:
==Final variables==
A '''final [[Variable (programming)|variable]]''' can only be initialized once, either via an initializer or an assignment statement. It does not need to be initialized at the point of declaration: this is called a "blank final" variable. A blank final instance variable of a class must be definitely assigned in every constructor of the class in which it is declared; similarly, a blank final static variable must be definitely assigned in a static initializer of the class in which it is declared; otherwise, a compile-time error occurs in both cases.<ref>Java Language Specification #8.3.1.2.</ref> (Note: If the variable is a reference, this means that the variable cannot be re-bound to reference another object. But the object that it references is still [[mutable object|mutable]], if it was originally mutable.)
Line 96 ⟶ 94:
=== Final variables in nested objects ===
Final variables can be used to construct trees of immutable objects. Once constructed, these objects are guaranteed not to change anymore. To achieve this, an immutable class must only have final fields, and these final fields may only have immutable types themselves. Java's primitive types are immutable, as are strings and several other classes.
Line 127 ⟶ 124:
===Final and inner classes===
When an anonymous [[inner class]] is defined within the body of a method, all variables declared <code>final</code> in the scope of that method are accessible from within the inner class. For scalar values, once it has been assigned, the value of the <code>final</code> variable cannot change. For object values, the reference cannot change. This allows the Java compiler to "capture" the value of the variable at run-time and store a copy as a field in the inner class. Once the outer method has terminated and its [[call stack|stack frame]] has been removed, the original variable is gone but the inner class's private copy persists in the class's own memory.
Line 154 ⟶ 150:
===Blank final===
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=https://archive.org/details/javainnutshelld100flan|edition=2nd|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|url-access=registration}}</ref><ref>{{cite web|url=http://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.12.4|title=Chapter 4. Types, Values, and Variables|year=2015|work=The Java® Language Specification (Java SE 8 Edition)|publisher=Oracle America, Inc.|access-date=23 Feb 2015}}</ref> Previous to Java 1.1, a final variable was required to have an initializer. A blank final, by definition of "final", can only be assigned once. i.e. it 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 name="define_assignment">{{cite web|url=http://docs.oracle.com/javase/specs/jls/se8/html/jls-16.html|title=Definite Assignment|year=2015|work=The Java® Language Specification (Java SE 8 Edition)|publisher=Oracle America, Inc.|access-date=29 Oct 2016}}</ref>
Line 196 ⟶ 191:
{{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". Nonetheless, casting away constness and then modifying the object results in [[undefined behavior]] if the object was originally declared <code>const</code>. Java's <code>final</code> is a strict rule such that it is impossible to compile code that directly breaks or bypasses the final restrictions. Using [[Reflection (computer programming)#Java|reflection]], however, it is often possible to still modify final variables. This feature is mostly made use of when [[Serialization|deserializing]] objects with final members.
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>.
==References==
{{reflist}}
|