Hello,
I tried to initialize the static final variable in the constructor. But i get a compile time error.
Well, i know we can initialize a static final variable during its declaration and also using static block initializers.
I tried initializing the final static variable in the constructor with an example and it gave me compiler errors.
1: class Test{
2: final static int finalvar;
3: public
Test(){
4: finalvar = 10;
5: }
6: public static void main(
String[] args){
7: Test Testobj = new Test();
8: System.out.println("Final variable value is "+finalvar);
9:}
10:}
following is the compiler error i get.
"Blank final variable 'finalvar' may not have been initialized. It must be assigned a value in a initializer, or in every constructor."
"Can't assign a second value to a blank final variable: finalvar"
on line# 4.
I would appreciate if someone could clear my doubt.
Thanks in advance,
Sunitha. S