• 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

initializing a static final variable

 
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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. . . )
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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???
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic