This page contains a list of user images about Final which are relevant to the point and besides images, you can also use the tabs in the bottom to browse Final news, videos, wiki information, tweets, documents and weblinks.
THE LEGEND OF ZELDA RAP [MUSIC VIDEO]WATCH BLOOPERS & MORE: http://bit.ly/ZELDAxtras DOWNLOAD THE SONG: http://smo.sh/13NrBp8 DOWNLOAD UNCENSORED SONG: http://smo.sh/WMYpsf GET LEGEND OF SMOSH T...
David Guetta - Titanium ft. SiaFrom the album Nothing But The Beat Ultimate - Download on iTunes here: http://smarturl.it/NBTBiTunes?IQid=vevo Featuring Sia, Ne-Yo, Akon, Nicki Minaj, Flo ...
MACKLEMORE & RYAN LEWIS - THRIFT SHOP FEAT. WANZ (OFFICIAL VIDEO)Thrift Shop on iTunes: http://itunes.apple.com/us/album/thrift-shop-feat.-wanz-single/id556955707 The Heist physical deluxe edition: http://www.macklemoremer...
David Guetta - She Wolf (Falling To Pieces) ft. SiaFrom the album Nothing But The Beat Ultimate - Download on iTunes here: http://smarturl.it/NBTBiTunes?IQid=vevo Featuring Sia, Ne-Yo, Akon, Nicki Minaj, Flo ...
Rihanna - Rehab ft. Justin TimberlakeMusic video by Rihanna performing Rehab. YouTube view counts pre-VEVO: 19591123. (C) 2007 The Island Def Jam Music Group.
Man of Steel - Official Trailer 3 [HD]http://manofsteel.com http://www.facebook.com/manofsteel In theaters June 14th. From Warner Bros. and Legendary Pictures comes "Man of Steel", starring Henry...
P!nk - Just Give Me A Reason ft. Nate RuessFrom the Grammy Nominated album The Truth About Love available now - http://smarturl.it/tal Music video by P!nk featuring Nate Ruess performing Just Give Me ...
One Direction - Little ThingsTAKE ME HOME The brand new album out now! Featuring Live While We're Young and Little Things. iTunes: http://smarturl.it/takemehome1D Amazon: http://amzn.to/...
Einstein vs Stephen Hawking -Epic Rap Battles of History #7Download this song: http://bit.ly/EpicRap7 New ERB merch: http://bit.ly/MNwYxq Tweet this Vid-ee-oh: http://clicktotweet.com/TpUg9 Hi. My name is Nice Peter,...
Rihanna - Stay ft. Mikky EkkoDownload "Stay" from Unapologetic now: http://smarturl.it/UnapologeticDlx Music video by Rihanna performing Stay ft. Mikky Ekko. © 2013 The Island Def Jam Mu...
David Guetta - Just One Last Time ft. Taped Rai"Just One Last Time" feat. Taped Rai. Available to download on iTunes including remixes of : Tiësto, HARD ROCK SOFA & Deniz Koyu http://smarturl.it/DGJustOne...
What Caffeine DoesPlease subscribe to my channel and my vlog channel! I make new videos here every Wednesday and make vlogs during my majestical daily life. JennaMarbles Jenna...
MACKLEMORE & RYAN LEWIS - CAN'T HOLD US FEAT. RAY DALTON (OFFICIAL MUSIC VIDEO)Macklemore & Ryan Lewis present the official music video for Can't Hold Us feat. Ray Dalton. Can't Hold Us on iTunes: https://itunes.apple.com/us/album/cant-...
Drive Thru Invisible Driver PrankLearn Magic at http://www.penguinmagic.com Instagram: http://instagram.com/themagicofrahat Twitter: http://twitter.com/magicofrahat Facebook Group: http://ww...
Draw My Life- Jenna MarblesThis video accidentally turned out kind of sad, ME SO SOWWY IT NOT POSED TO BE SAD WHO WANTS HUGS AND COOKIES? Also, FYI for anyone attempting this, it takes...
Rihanna - DiamondsPre-order new album Unapologetic, out worldwide Monday, November 19: http://smarturl.it/UnapologeticDlx Music video by Rihanna performing Diamonds. ©: The Is...
Rihanna - Pon de Replay (Internet Version)Music video by Rihanna performing Pon de Replay. YouTube view counts pre-VEVO: 4166822. (C) 2005 The Island Def Jam Music Group.
How Diets WorkPlease subscribe to my channel and my vlog channel! I make new videos here every Wednesday and make vlogs during my majestical daily life. JennaMarbles Jenna...
Pokemon Theme Song REVENGE!BLOOPERS & DELETED SCENES: http://bit.ly/POKEMONbloopers DOWNLOAD THE SONG: http://smo.sh/WMZqR5 CHECK OUT THIS POKESMOSH SHIRT: http://smo.sh/YMRJ5e COLLECT...
Fast Food Lasagna - Epic Meal TimeLIKE/FAV We got 45 burgers, a whole bunch of liquor and bacon.... this is Fast Food Lasagna. Buy TSHIRTS!! Click Here! http://shop.epicmealtime.com/ Like on ...
In the Java programming language, the final keyword is used in several different contexts to define an entity which cannot later be changed.
Contents |
Final classes [edit]
A final class cannot be subclassed. This is done for reasons of security and efficiency. Accordingly, many of the Java standard library classes are final, for example java.lang.System and java.lang.String. All methods in a final class are implicitly final.
Example:
public final class MyFinalClass {...} public class ThisIsWrong extends MyFinalClass {...} // forbidden
Restricted subclasses are often referred to as "soft final" classes.[1]
Final methods [edit]
A final method cannot be overridden by subclasses. 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.[2]
Example:
public class MyClass { public void myMethod() {...} public final void myFinalMethod() {...} } public class AnotherClass extends MyClass { public void myMethod() {...} // Ok public final void myFinalMethod() {...} // forbidden }
A common misconception is that declaring a class or method final 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 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.[3]
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 [edit]
A final 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.[4] (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, if it was originally mutable.)
Unlike the value of a constant, the value of a final variable is not necessarily known at compile time. It is considered good practice to represent final primitive type variables in all uppercase, using underscore to separate words.[5]
Example:
public class Sphere { // pi is a universal constant, about as constant as anything can be. public static final double PI = 3.141592653589793; public final double RADIUS; public final double XPOS; public final double YPOS; public final double ZPOS; Sphere(double x, double y, double z, double r) { RADIUS = r; XPOS = x; YPOS = y; ZPOS = z; } [...] }
Any attempt to reassign RADIUS, XPOS, YPOS, or ZPOS will meet with a compile error. In fact, even if the constructor doesn't set a final variable, attempting to set it outside the constructor will result in a compilation error.
To illustrate that finality doesn't guarantee immutability: suppose we replace the three position variables with a single one:
public final Position pos;
where pos is an object with three properties pos.x, pos.y and pos.z. Then pos cannot be assigned to, but the three properties can, unless they are final themselves.
Like full immutability, finality of variables has great advantages, especially in optimization. For instance, Sphere will probably have a function returning its volume; knowing that its radius is constant allows us to memoize the computed volume. If we have relatively few Spheres and we need their volumes very often, the performance gain might be substantial. Making the radius of a Sphere final informs developers and compilers that this sort of optimization is possible in all code that uses Spheres.
Final and inner classes [edit]
When an anonymous inner class is defined within the body of a method, all variables declared final in the scope of that method are accessible from within the inner class. Once it has been assigned, the value of the final variable 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 stack frame has been removed, the original variable is gone but the inner class's private copy persists in the class's own memory.
import javax.swing.*; public class FooGUI { public static void main(String[] args) { //initialize GUI components 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() { @Override public void run() { jf.pack(); //this would be a compile-time error if jf were not final jf.setLocationRelativeTo(null); jf.setVisible(true); } }); } }
Blank final [edit]
The blank final, which was introduced in Java 1.1, is a final variable whose declaration lacks an initializer.[6][7] 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.[8]
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:
for (final SomeObject obj : someList) { // do something with obj }
Since obj is only actually assigned in a single statement, it is legal even though obj will change with each iteration.[9]
Difference from the C++ const type qualifier [edit]
The C++ parallel to Java's final would be a reference in that once it is initialized, the data being referenced can be changed, but the reference itself can not.
Another C++ parallel is SomeClass * const ptr where the contents being referenced can be modified, but the reference itself can not without casting. This is different from the C++ const SomeClass * ptr 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 unconst reference. Java's final is a strict rule in that you can't write valid code that compiles that breaks or simply bypasses the final restrictions.
References [edit]
- ^ "How to Use the Eclipse API". The Eclipse Foundation. 2010. Retrieved 23 July 2010.
- ^ Writing Final Classes and Methods
- ^ Java theory and practice: Is that your final answer?
- ^ Java Language Specification #8.3.1.2.
- ^ http://geosoft.no/development/javastyle.html
- ^ Flanagan, David (May 1997). "Chapter 5 Inner Classes and Other New Language Features:5.6 Other New Features of Java 1.1". Java in a Nutshell (2nd Edition ed.). O'Reilly. ISBN 1-56592-262-X.
- ^ "Types, Values, and Variables". Java Language Specification Chapter 4. Sun Microsystems, Inc. 2000. Retrieved 23 July 2010.
- ^ "Definite Assignment". Java Language Specification. Sun Microsystems, Inc. 2000. Retrieved 23 July 2010.
- ^ Pattis, Richard E. "More Java". Advanced Programming/Practicum 15-200. School of Computer Science Carnegie Mellon University. Retrieved 23 July 2010.
External links [edit]



Research