Reputation: 2105
I have three constant floats, whose values I want to be assigned to in the constructor of the class.
I declare all three constants before the constructor. As an experiment, I set different modifiers to each:
public static float defaultdim;
public static final float maxdim;
public final float mindim;
My IDE, Eclipse, detects an error for the second constant, maxdim, namely:
the blank final field maxdim may not have been initialized
What puzzles me is not why the error appears but why the error doesn't appear in the case of mindim, which is also final, considering the error message is connected to the fact that the variable is final. In other words, the error is there because a final variable is not initialized during declaration. Both variables are final and both are not initialized, but the error appears in only one case.
The two variables differ by the modifier static, which suggests that would be the cause of the error. However, I do not see why this would be the case since no error appears in the case of defaultdim, which is only static, not final and the error itself does not refer to the modifier static. Could anyone explain the reason behind it to me?
Upvotes: 3
Views: 2722
Reputation: 23012
public static float defaultdim;
static
field which will be initialized with default value 0.0F
in case of float
if you have not specified the value explicitly. (i.e. In case of int
it will have 0
) Moreover, it's not final
.
Next both declarations are considered as blank final fields declarations which lacks the initializer.
public static final float maxdim;
Every final
field must only be assigned once and atleast before making use of it. static
variables initialized when class is loaded and due to it's final
it must have value while the class
is being loaded and yet you receive the error in case of static final
(constant).
public final float mindim;
In above case you have not initialized it anywhere in code, what compiler thinks you will not have this variable initialized anymore in future as well. You must initialize it before making any use of it. You can avoid compile time error in this case by initializing it in constructor.
public Test(float mindim) {
this.mindim = mindim;
}
By doing this you have assured compiler that mindim
will be initialized before being used anywhere in code, note that it's not static
and it will have different copy for each Object
you create for your class.
But what if I need to assign a value to a static final field based on an argument passed to the constructor of the class?
No. static final
field is Class
related thing while constructor is Object
thing. Your constant should be initialized when class
is loaded while your constructor will be called when you create Object
of class with new
. There is no meaning of initializing constant in constructor. If it can vary with every Object than it's definitely not a constant. Yes you can keep your field only final
and assign value based on constructor parameter because that way it will have final
value for each Object
.
Upvotes: 4