instead use:
private final int j = 1;
Main Topics
Browse All TopicsHere 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();
}
}
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
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()
Wow, there are a lot of misleading, contradicting answers here. This only brings to light that "static" and "final" are really confusing, especially when they interact.
You DO NOT have to assign a value to a final variable when you declare it. The "final" keyword just means that you can (and must) assign its value exactly once. Where you are allowed to assign the value depends on if it is static or not (as well as its access modifier).
private final int j;
The value MUST be assigned ONCE, either here in the declaration or in each constructor. It is NOT automatically assigned a value of 0 (zero). Also, since it is not static, there is a distinct value for each instance object, so it makes sense that it must be assigned in the constructor.
private static int k;
The value may be assigned from anywhere in this class. Since it is not final, the value may be assigned any number of times, but since it is static, this value is stored at the class-level and universally available to all instances. If left unassigned, it is assumed to be 0 (zero).
private static final int i;
The value MUST be assigned ONCE, either here in the declaration or in a "static" class-level block. It is NOT automatically assigned a value of 0 (zero).
So, to review:
"final": per-instance value, must be assigned once per instance.
"final" + "static": per-class value, must be assigned once per class.
"static": per-class value, can be assigned any number of times per class.
[neither modifier]: per-instance value, can be assigned any number of times per instance.
Before Java can create an instance of a class, it must first load and initialize that class. One step of class initialization is assigning values to all its static fields. If they're static AND final, those values can't change once they're assigned.
So, to answer your question, the reason you can't assign a value to a static final field in a constructor is that by the time you call the constructor, it should already have a value assigned which cannot be changed.
Hope this helps!
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
Business Accounts
Answer for Membership
by: objectsPosted on 2007-10-16 at 15:12:31ID: 20089496
because the declaration of it has already implicitly initilized it to 0
and u cannot change value of final vars