• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

doubt in initializing the static final variable.

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you declare a static final variable it must be initialized. It is made final right there so if you don't initialize it, it will not have a value. when you try to initialize it two lines down you get an error because it is final and cannot be changed. I hope this helps.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the variable is not static, but just final
then it should be initialized in all constructors.
 
Sunitha Sounderrajan
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Naoh Carroll,
Thanks for the reply.
I tried to run the following with static initializers and the code ran fine.
i am posting the code below.
class Test{
final static int finalvar;
public Test(){}
static {finalvar = 10;}
public static void main(String[] args){
Test Testobj = new Test();
System.out.println("Final variable value is "+finalvar);
}
}
In this case, i initialize the static final variable in a different place and it works.
i am not yet clear ...
pls explain.
Thanks,
Sunitha. S
When you declare a static final variable it must be initialized. It is made final right there so if you don't initialize it, it will not have a value. when you try to initialize it two lines down you get an error because it is final and cannot be changed. I hope this helps.
 
Leverager of our synergies
Posts: 10065
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sunitha,
I remember Maha Anna said something about it and I found her message in my notes:
"one correction. ( I think it is important )
static final vars MUST be initialized either in declaration or in one of static initialization blocks.NOT in constructors.The compiler does not wait until the construction of the object.
But instance final vars may be initialized in the constructor. If it is initialized in constructor, ALL CONSTRUCTORS MUST initialize the instance final var.
In both cases once it is initialized we can not reassign a new value to them."
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is my understanding.
You declare a static final variable in the class definition. If you try to intialize using statis intialiser its fine. Because static intializers are loaded and initialised as soon as the class is loaded. So in sunitha example "finalvar" will be ok if she had intiliazed at the place where she declared or using a static initializer. But it gives an error if u try using constructor. The REASON is when the class variables are declared it will assign to default values unless it doesnt have one. So here in ur example it will assign it to zero. Again u r trying to assign a second value to a final variable using constructor which is wrong. If u assign it using static initializer it will initialize as soon the class is loaded .. so no problem. but here it will be an error. Hope this helps.
I dont agree with what Mr. Noah has said that static final variable must be initialized at the point of declaration. No it can also be initialized using static initializers. )
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just my two cents ... but, <code>static</code> means the variable applies to the <code>class</code> and <code>final</code> means the value can't be changed once it's initialized.
<code>constructors</code> are called each time an <code>instance</code> is created.
If you try to initialize a <code>static</code> variable in a constructor you're trying to treat it as an <code>instance</code> variable ie changing it for every instance. With a <code>static final</code> variable you're also trying to initialize it repeatedly.
Jane
 
Sunitha Sounderrajan
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone,
Thanks for the explanations..now i understand why static final variables cannot be initialized in the constructors. I appreciate the interest took by everyone who replied to clear my doubt.
Have a great day.
Sunitha. S
 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If a class has not been initialized yet and we use its static variable which is something like
static int i = 5;
Does it cause the initialization if the class? Or does it cause only loading of the class.
The jls1 appears to say that it will be, but jls2, to my understanding says it will not cause initialization.
 
Sunitha Sounderrajan
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
mohit,
A static variable can be initialized and used/displayed if a class has not been initialized also. i tried with an example and it works. I tried to display the static variable before i created an instance and the static variable value is printed before the constructor is invoked.
i think i answered your question.
Sunitha. S

[This message has been edited by Sunitha Sounderrajan (edited October 03, 2000).]
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Mihit
you are right in thinking that static variables are assigned values at class loading time, and not initialization. take a look at this:
class AAA
{
static int a;
static
{
System.out.println("inside Static");
a=5*58;
}
AAA()
{
System.out.println("Inside Con");
}
}
class Tester
{
public static void main(String s[])
{
System.out.println("Inside main");
System.out.println(AAA.a);
}
}
try executing this code and see the sequence.
hope this helps
Sanjeev Verma
PS: BTW, should I postpone the date of my exam (11th OCT). i am getting the jitters with all these stories floating around.
 
mohit joshi
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String st = xt+"you";
static String xt =yt + "there"; // line 2
final static String yt = "hello";

the above code fails to complile at line 2 saying "Can't make forward reference to yt in class XXX.".Here is a section of JLS 8.3.2.1 Initializers for Class Variables:

"One subtlety here is that, at run time, static variables that are final and that are initialized with compile-time constant values are initialized first. This also applies to such fields in interfaces (�9.3.1). These variables are "constants" that will never be observed to have their default initial values (�4.5.5), even by devious programs."
This says that final static variables are initialized first. So i find it odd that my code fails to compile. Can anyone explain?
 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mohit,
What JLS clearly talks about [b]RUNTIME[\b] scenario and NOT that of compile time.
During compilation, the compiler is yet encounter the static final var. So, it complains about the fwd reference.
Once compilation is successful, all the static final vars are resolved during compile time. At run time, when the class is loaded, these static final vars are initialized first with these compile time constants.
Hope this helps
------------------

- Sathvathsan Sampath
 
mohit joshi
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Sampath, you were quite clear about that.
I was confusing between runtime and compile time.
 
You didn't tell me he was so big. Unlike this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic