Link to home
Start Free TrialLog in
Avatar of perfect_tranquility
perfect_tranquilityFlag for United States of America

asked on

initialization of final static variable in a constructor

Here is the code: What is the issue with the "target line" ? Why Java does not allow the initialization of  a final static variable in a constructor?

Thanks,
R

public class class1 {
      private final int j ;
      private static int k;
      private final static int i;

class1()
{
//i=444; /* target line */
j=1;
k=22;
}

static{
      i=1;
      k=1;
       }

public static void main(String[] args) {new class1();
}
}

Avatar of Mick Barry
Mick Barry
Flag of Australia image

because the declaration of it has already implicitly initilized it to 0
and u cannot change value of final vars
instead use:
      private final int j = 1;
static final variable is a variable with constant value which belongs to class (not to objects).

It must be initialized before calling any constructor.
If it was intialized in every constructor it wouldn't be final (constant).
So you have to initialize it in the place of it's definition.
sorry, wrong line:

      private final static int i = 1;
Avatar of TheMajestic
TheMajestic

The thing is  

  The Static variables can be accessed without creating the object . i.e., 4They r intialized before creating the objects. if u r trying to intializing a variable of static inside the constructor its not possible.
rather u can use final alone.
     
The very nature of the key word final means that once the variable is created, that is its final state. It cannot be changed after this. As you did not set a value to the i variable when creating it, it was automatically assigned the value of 0. However, in the constructor you have then tried to change it to the value of 1 which is not allowed. In order to rectify this simply remove i = 444; from the constructor and in the class variable section make a slight modification so it reads as below:

public class class1 {
      private final int j ;
      private static int k;
      private final static int i = 144;

class1()
ASKER CERTIFIED SOLUTION
Avatar of malfunction84
malfunction84
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of perfect_tranquility

ASKER

i started looking at the replies top down. I realized with minimal effort that there WAS NO DEFAULT value assigned to any final variable. either initialize it in a static block or just initialize it at the point of declaration.


it is surprising those whose answers run contrary to how java fundamentally works have not come back to comment and/or  update their comments.
 malfunction84 your id may say so but your performance has been fantastic. you have put in the effort and won!

R


also it's not always I award all the points to one answer. this case is a pointer to the fact that the selected answer is not only correct  but thorough!

R
Sorry Uros, your answer is theoretically correct too. I will mark you surely in your next response and make up.

R
Thanks, tranq.  I think you can contact the EE administrators if you want to distribute points to Uros for this question.  The Community forums are the place to go for that sort of thing.