Final (Java): Difference between revisions

Content deleted Content added
m Reverted 1 edit by 204.11.185.30 identified as test/vandalism using STiki
mNo edit summary
Line 2:
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 cannot later be changed.
 
== Final classes ==
 
A '''final [[Class (computer science)|class]]''' cannot be subclassed. ThisDoing isthis donecan for reasons ofconfer security and efficiency. Accordinglybenefits, so many of the Java standard library classes are final, for example {{Javadoc:SE|package=java.lang|java/lang|System}} and {{Javadoc:SE|package=java.lang|java/lang|String}}. All methods in a final class are implicitly final.
 
Example:
Line 15:
Restricted subclasses are often referred to as "soft final" classes.<ref>{{cite web|url=http://www.eclipse.org/articles/article.php?file=Article-API-Use/index.html|title=How to Use the Eclipse API|year=2010|publisher=The Eclipse Foundation.|accessdate=23 July 2010}}</ref>
 
== Final methods ==
 
A '''final [[Method (computer science)|method]]''' cannot be [[Method overriding|overridden]] or hidden by subclasses.<ref>[http://docs.oracle.com/javase/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>
Line 21:
Example:
<source lang="java">
public class Base
{
public void m1()
public final void m2() {...}
 
Line 30:
}
 
public class Derived extends Base
{
public void m1() {...} // Ok, overriding Base#m1()
Line 40:
</source>
 
A common misconception is that declaring a class or method as <code>final</code> improves efficiency by allowing the compiler to directly insert the method inline wherever it is called. In fact, the compiler is unable to do this because the method is loaded at runtime and might not be the same version as the one that was just compiled. Only the runtime environment and [[Just-in-time compilation|JIT]] compiler have the information about exactly which classes have been loaded, and are able to make better decisions about when to inline, whether or not the method is final.<ref>[http://www.ibm.com/developerworks/java/library/j-jtp1029.html Java theory and practice: Is that your final answer?]</ref>
 
An exception to this are machine code compilers (those who build directly executable, platform specific machine code). In this case, when using static linking, the compiler can safely assume that the methods, and also compiletime-computable variables, can be inlined.
 
== 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 at the end of 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 55:
 
// pi is a universal constant, about as constant as anything can be.
public static final double PI = 3.141592653589793;
 
public final double radius;
Line 98:
final JFrame jf = new JFrame("Hello world!"); //allows jf to be accessed from inner class body
jf.add(new JButton("Click me"));
 
// pack and make visible on the Event-Dispatch Thread
SwingUtilities.invokeLater(new Runnable() {
Line 124:
</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-20015–200|publisher=School of Computer Science [[Carnegie Mellon University]]|accessdate=23 July 2010}}</ref>
 
==Difference from the C++ <code>const</code> type qualifier==
Line 134:
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 unconst reference. Java's final is a strict rule such that you can't write a valid code that compiles, breaks or simply bypasses the final restrictions.
 
== References ==
 
{{reflist}}