File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question about the assignments of static final variables

 
Kate Shaw
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you are... and here's how you get around that little final static problem...



-Nate
 
Kate Shaw
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you.
 
shilpa kulkarni
Ranch Hand
Posts: 87
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 158
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic