aspose file tools*
The moose likes Beginning Java and the fly likes Question about the assignments of static final variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question about the assignments of static final variables" Watch "Question about the assignments of static final variables" New topic
Author

Question about the assignments of static final variables

Kate Shaw
Greenhorn

Joined: Oct 05, 2001
Posts: 7
Hi!
Here is my question:
These following codes both compile.

But this one does not:

Does anyone know why it do not compile?
I'm thinking about it for almost one week,and have asked many people. But I still donnot know why. None of the answers are reasonable. Would somebody help me or should I post this message on the forum: Java in General (intermediate)? Thanks.
[This message has been edited by Kate Shaw (edited October 05, 2001).]
[This message has been edited by Cindy Glass (edited October 05, 2001).]
ashyabhi
Greenhorn

Joined: Oct 05, 2001
Posts: 1
hi,

actually u r missing some basic of java.
any variable with keyowrd final are once initilize can not change or manupulated.
that's whay ur coad is not compiled...
Kate Shaw
Greenhorn

Joined: Oct 05, 2001
Posts: 7
Originally posted by ashyabhi:
hi,

actually u r missing some basic of java.
any variable with keyowrd final are once initilize can not change or manupulated.
that's whay ur coad is not compiled...

Thank you very much.
Many people explained the question exactlly as you did.But if so, how to explain the fact that the following code compiles?
public class b{
�� final long y;
�� b(){y=10;}
}
Kate Shaw
Greenhorn

Joined: Oct 05, 2001
Posts: 7
Finally��I know the answer.
It is a compile-time constant referring to Thinking In Java.
A static final variable means that it is a variable of the whole class and cannot be changed. If it can be assigned in the constructor, it will be changed every time when an instance of the class is created.Am I right?
[This message has been edited by Kate Shaw (edited October 05, 2001).]
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Yes, you are... and here's how you get around that little final static problem...



-Nate


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Kate Shaw
Greenhorn

Joined: Oct 05, 2001
Posts: 7
Thank you.
shilpa kulkarni
Ranch Hand

Joined: Jun 07, 2000
Posts: 87
Nathan,
Is there any advantage in doing it the way you have mentioned (in a static block), over just initializing 'y' when you declare it?
for eg. - static final int y = 10;
Michael Bruesch
Ranch Hand

Joined: Sep 23, 2001
Posts: 158
When the class is LOADED, the static variables are initialized first. So the static final variable that is not initialized gets a default value. When a class in INSTANTIATED, the class is already loaded and all static variables are assigned values. When you try to assign a value to a static final variable in a constructor, that variable has already been assigned a value and the compiler thinks you are trying to change a final variable. That is more or less correct, yes? And I'm kinda curious too, is there a difference between initializing the final variable in a static block vs. initializing the final variable on the same line it is declared?


Michael J Bruesch<br /><i>I code, therefore I am.</i>
 
 
subject: Question about the assignments of static final variables