aspose file tools*
The moose likes Java in General and the fly likes initializing a static final variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "initializing a static final variable" Watch "initializing a static final variable" New topic
Author

initializing a static final variable

Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040

When I compile the foll code., I get the error:

SO I uncomment the line in the constructor and get these errors:

Isin't that contradictory. or am I missing something?
Thanks for your response.
regds.
- satya


Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
You have two variables that are final, and not initialized, thereby guaranteeing that they can NEVER be initialized, making them worthless.
static final int s_f_const;
final int f_const;
Then you tried to change the value of a final (as in unchangable) variable with
s_f_const = 101;
What the compiler is trying to tell you is that you have to initialize the final variables AT THE TIME YOU DECLARE THEM (cuz after that you are outta luck).
static final int s_f_const = 101;


"JavaRanch, where the deer and the Certified play" - David O'Meara
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040

Cindy:
not initialized, thereby guaranteeing that they can NEVER be
initialized, making them worthless

I do not agree to this stmt. You can initialize the final
member variable in the constructor
. It is perfectly valid.
Proof: Comment out the static final in the abv code and verify.
Then you tried to change the value of a final (as in unchangable) variable with
s_f_const = 101;

See thats not totally true again. Since in the first error, the
Java compiler tells me that I have to "assign a value...in
every constructor
"
When I provide a constructor, we know theres' no other (default)
constructor. So whats' wrong?
Now, there may not be practical use of it, but I don't want to
miss an opportunity to file a bug against java.
Thanks.
regds.
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040

finally, the JLS to my rescue....
IMHO, the compiler msg seems to be improperly worded.
The JLS is a little more clear on this issue.
Thanks.
- satya
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
OK, I stand corrected. If you declare the final variable, AND you insure that the variable is assigned a valid value in EVERY constructor then (in theory) you can initialize them later.
Also you can defer initialization of a final local variable (I have now learned).
However a STATIC final variable can't get initialized in a constructor - that's for making instances. It needs to be initialized in a static initializer block.

(HeHeHe - the people at Yale agree with my original post )
http://www.yale.edu/pclt/java/final.htm
(course it is kind of an OLD reference. . . )
Rob Acraman
Ranch Hand

Joined: Dec 03, 2000
Posts: 89
The problem could be that it's declared "static" rather than declared "final". Remember, static variables belong to the class rather than to any instance of the class.
Static methods can be invoked without having an instance of the class (eg. when "java finalTest" is run, I don't believe offhand that an instance of finalTest is created). However, if an instance of the class is not created, then the constructors will never have been run - so the static variables will never have been set. This is why the compiler (correctly) complains that you are using a variable that may never have been assigned.
Solution: use "static final int s_f_const = 110;" as suggested, or use a static initialiser block if you want to embed some logic. Offhand (again, since I'm not at my home PC), I believe the syntax is :
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040

Rob:
I agree to what ever you said. I mena it. 100 %.
However, what caught me by surprise is the error message:

See the sentence formation:
It must be assigned a value in an initializer,
this I din't do so
or in every constructor.
and when I did this, it gave me two errors.
Thats when I bailed out to see what I am missing.
While the compiler does the right thing, the error msg
IMHO, is mis-leading (atleast when it comes to people like me).
regds.
- satya

Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521

Well, I have to agree with THAT. The compiler message had me looking in the wrong direction for quite a bit, before I got back to the code itself.
Of course the problem is PROBABLY that the compiler parses the code sequentially, and it ran into the "final variable" issue before noticing the "static final" issue, and therefore just spouted out the first thing that it thought of.
You want to try and suggest to SUN that it's compiler could use some improvement???
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: initializing a static final variable