final : Java Glossary
home F words local find no local find frame, full screen Google search web for topic jump to footer translate with Babelfish 2007-09-25 by Roedy Green ©1996-2008 Canadian Mind Products
Go to : punctuation 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z (all)
final
final means, this value won’t be changed hereafter.

Advantages of using final

final is one of the most under-used features of Java. Whenever you compute a value and you know it will never be changed subsequently put a final on it. Why?
  • final lets other programmers (or you reviewing your code years later) know they don’t have to worry about the value being changed anywhere else.
  • final won’t let you or someone else inadvertently change the value somewhere else in the code, often by setting it to null. final helps prevent or flush out bugs. You can always remove it later.
  • final helps the compiler generate faster code.
  • I have got into the habit of using final everywhere, even on local variables, and if I am in doubt, I use final on every declaration then take it off when the compiler points out that I modified it elsewhere. When I read my own code, a missing final is a red flag there is something complicated going on to compute a value.
  • A little known feature of Java is blank finals. You can declare member variables final, but not declare a value. This forces all constructors to initialise the blank final variables. A final idiom I often use looks like this:

final contexts

The term final is used in a number of contexts. static final variables are close to constants in other languages. final classes may not be subclassed. final methods may not be overridden. On methods private implies final, but on variables does not. Marking things final has two purposes: efficiency and safety. The compiler can perform various optimisations knowing the value cannot change. Hotspot and optimising compilers now do this anyway, whether or not you declare methods final, so using final purely for efficiency is no longer recommended. The compiler can also check to ensure you do not inadvertently attempt to change the value after computing its value once where it is defined.

You can have both final instance and final static variables. final statics are more common. When you know the value of a constant at compile time you might as well make it static. It takes up less room, just one copy per class instead of one copy per object. It is also faster to access a static constant than an instance constant. However, if you don’t know the value of the constant until instantiation time, you have to make it an instance constant.

final is not the same as C++ const

If you have a final reference to an object or array, it does not stop you from changing the fields in the object or elements of the array. final just stops you from pointing that reference variable to a different object or array. If you want to protect the object from changes, you must make it immutable, namely remove any setter methods from its class definition. Java’s final is not as flexible and powerful as C++ const, however, Java’s final is less error prone.

Learning More

If you are a language lawyer, you might enjoy digging into the JLS for its academic descriptions on how final is used.

CMP_homejump to top
CMP logo
feedback Please email your feedback for publication, errors, omissions, broken/redirected link reports
and suggestions to improve this page to Roedy Green : feedback email
made with CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[65.110.21.43]
Your face IP:[38.103.63.16] The information on this page is for non-military use only.
You are visitor number 98,806. Military use includes use by defence contractors.
You can get a fresh copy of this page from: or possibly from your local J: drive (Java virtual drive/Mindprod website mirror)
http://mindprod.com/jgloss/final.html J:\mindprod\jgloss\final.html