In Java we use final
keyword with variables to specify its values are not to be changed.
But I see that you can change the value in the constructor / methods of the class. Again, if the variable is static
then it is a compilation error.
Here is the code:
import java.util.ArrayList;
import java.util.List;
class Test {
private final List foo;
public Test()
{
foo = new ArrayList();
foo.add("foo"); // Modification-1
}
public static void main(String[] args)
{
Test t = new Test();
t.foo.add("bar"); // Modification-2
System.out.println("print - " + t.foo);
}
}
Above code works fine and no errors.
Now change the variable as static
:
private static final List foo;
Now it is a compilation error. How does this final
really work?
If you make
foo
static, you must initialize it in the class constructor (or inline where you define it) like the following examples.Class constructor (not instance):
Inline:
The problem here is not how the
final
modifier works, but rather how thestatic
modifier works.The
final
modifier enforces an initialization of your reference by the time the call to your constructor completes (i.e. you must initialize it in the constructor).When you initialize an attribute in-line, it gets initialized before the code you have defined for the constructor is run, so you get the following outcomes:
foo
isstatic
,foo = new ArrayList()
will be executed before thestatic{}
constructor you have defined for your class is executedfoo
is notstatic
,foo = new ArrayList()
will be executed before your constructor is runWhen you do not initilize an attribute in-line, the
final
modifier enforces that you initialize it and that you must do so in the constructor. If you also have astatic
modifier, the constructor you will have to initialize the attribute in is the class' initialization block :static{}
.The error you get in your code is from the fact that
static{}
is run when the class is loaded, before the time you instantiate an object of that class. Thus, you will have not initializedfoo
when the class is created.Think of the
static{}
block as a constructor for an object of typeClass
. This is where you must do the initialization of yourstatic final
class attributes (if not done inline).Side note:
The
final
modifier assures const-ness only for primitive types and references.When you declare a
final
object, what you get is afinal
reference to that object, but the object itself is not constant.What you are really achieving when declaring a
final
attribute is that, once you declare an object for your specific purpose (like thefinal List
that you have declared), that and only that object will be used for that purpose: you will not be able to changeList foo
to anotherList
, but you can still alter yourList
by adding/removing items (theList
you are using will be the same, only with its contents altered).Final keyword has a numerous way to use:
Other usage:
A static class variable will exist from the start of the JVM, and should be initialized in the class. The error message won't appear if you do this.
The
final
keyword in java is used to restrict the user. The javafinal
keyword can be used in many context. Final can be:The
final
keyword can be applied with the variables, afinal
variable that has no value, is called blankfinal
variable or uninitializedfinal
variable. It can be initialized in the constructor only. The blankfinal
variable can bestatic
also which will be initialized in thestatic
block only.Java final variable:
If you make any variable as
final
, you cannot change the value offinal
variable(It will be constant).Example of
final
variableThere is a final variable speedlimit, we are going to change the value of this variable, but It can't be changed because final variable once assigned a value can never be changed.
Java final class:
If you make any class as
final
, you cannot extend it.Example of final class
Java final method:
If you make any method as final, you cannot override it.
Example of
final
method (run() in Honda cannot override run() in Bike)shared from: http://www.javatpoint.com/final-keyword
Suppose you have two moneyboxes, red and white. You assign these moneyboxes only two children and they are not allowed interchange their boxes. So You have red or white moneyboxes(final) you cannot modify the box but you can put money on your box.Nobody cares (Modification-2).
When you make it static final it should be initialized in a static initialization block
final
is a reserved keyword in Java to restrict the user and it can be applied to member variables, methods, class and local variables. Final variables are often declared with thestatic
keyword in Java and are treated as constants. For example:When we use the
final
keyword with a variable declaration, the value stored inside that variable cannot be changed latter.For example:
Note: A class declared as final cannot be extended or inherited (i.e, there cannot be a subclass of the super class). It is also good to note that methods declared as final cannot be overridden by subclasses.
Benefits of using the final keyword are addressed in this thread.