[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

initialization of final static variable in a constructor

Posted on 2007-10-16
11
Medium Priority
?
10,521 Views
Last Modified: 2013-12-22
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();
}
}

0
Comment
Question by:perfect_tranquility
  • 3
  • 3
  • 2
  • +3
11 Comments
 
LVL 92

Expert Comment

by:objects
ID: 20089496
because the declaration of it has already implicitly initilized it to 0
and u cannot change value of final vars
0
 
LVL 92

Expert Comment

by:objects
ID: 20089503
instead use:
      private final int j = 1;
0
 
LVL 7

Expert Comment

by:UrosVidojevic
ID: 20089507
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.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Expert Comment

by:objects
ID: 20089509
sorry, wrong line:

      private final static int i = 1;
0
 
LVL 4

Expert Comment

by:TheMajestic
ID: 20090565
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.
     
0
 
LVL 1

Expert Comment

by:Sephster
ID: 20097695
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()
0
 
LVL 8

Accepted Solution

by:
malfunction84 earned 2000 total points
ID: 20120781
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!
0
 

Author Comment

by:perfect_tranquility
ID: 20159730
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


0
 

Author Comment

by:perfect_tranquility
ID: 20159734
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
0
 

Author Comment

by:perfect_tranquility
ID: 20162294
Sorry Uros, your answer is theoretically correct too. I will mark you surely in your next response and make up.

R
0
 
LVL 8

Expert Comment

by:malfunction84
ID: 20163389
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.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month18 days, 14 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question