aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt in initializing the static final variable. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt in initializing the static final variable." Watch "doubt in initializing the static final variable." New topic
Author

doubt in initializing the static final variable.

Sunitha Sounderrajan
Ranch Hand

Joined: Sep 12, 2000
Posts: 36
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
Noah Carroll
Ranch Hand

Joined: Sep 20, 2000
Posts: 96
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.
Binod
Greenhorn

Joined: Sep 19, 2000
Posts: 7
If the variable is not static, but just final
then it should be initialized in all constructors.
Sunitha Sounderrajan
Ranch Hand

Joined: Sep 12, 2000
Posts: 36
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.
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
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."

Uncontrolled vocabularies
"I try my best to make *all* my posts nice, even when I feel upset" -- Philippe Maquet
Anboo S
Greenhorn

Joined: Oct 03, 2000
Posts: 4
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. )
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
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


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Sunitha Sounderrajan
Ranch Hand

Joined: Sep 12, 2000
Posts: 36
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
mohit joshi
Ranch Hand

Joined: Sep 23, 2000
Posts: 243
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

Joined: Sep 12, 2000
Posts: 36
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).]
Sanjeev Verma
Ranch Hand

Joined: Aug 24, 2000
Posts: 87
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

Joined: Sep 23, 2000
Posts: 243
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?
Sathvathsan Sampath
Ranch Hand

Joined: Oct 03, 2000
Posts: 96
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


- Sathvathsan Sampath
mohit joshi
Ranch Hand

Joined: Sep 23, 2000
Posts: 243
Thanks Sampath, you were quite clear about that.
I was confusing between runtime and compile time.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doubt in initializing the static final variable.