Java Glossary : final

CMP home Java glossary home Menu no menu Last updated 2004-06-28 by Roedy Green ©1996-2004 Canadian Mind Products

Java definitions: 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

You are here : home : Java Glossary : F words : final.

final
final means, this value won't be changed hereafter. 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. private implies final. 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.

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.

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.

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. It just stops you from pointing that variable to a different object or array. If you want to protect the object, you must make it immutable.


CMP logo
CMP_home
home
Canadian Mind Products CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[24.87.56.253]
Your IP:[80.134.30.163]
You are visitor number 4321.
Please send errors, omissions and suggestions
to improve this page to Roedy Green.
You can get a fresh copy of this page from: or possibly from your local J: drive mirror:
http://mindprod.com/jgloss/final.html J:\mindprod\jgloss\final.html