• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static Final Variables Query

 
Sahil Kapoor
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As we all know that

"static final variables " are constants in JAVA.

Problem :- Why not only final variables are treated as constants, because we can not chabge the value of final variables once intialized , so they must be constatnt.

Thanks !!!
 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sahil can you clarify your question more clearly please. When you say the word constant, do you mean compile time constants or merely constants whose value don't change??
 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I got what Sahil wants to know
his question is

what is the difference between constants in java and compile time constants?
we know that final variables are compile time constants and static final are constants
what is the difference between the two?
 
Henry Wong
author
Marshal
Pie
Posts: 21420
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prasad Kharkar wrote:
what is the difference between constants in java and compile time constants?
we know that final variables are compile time constants and static final are constants
what is the difference between the two?


"Constants" in a java program are merely values that don't change. You can store them in final variables. You can store them in a static final variable. You can store them in a database. You can store them in a property file. etc. etc. etc.

Historically, program constants that are stored in a java class are stored as final static variables. Final because they don't change. And static because you only need one copy.



Now... as for compile time constants, that is different. There is actually special processing in the compiler to handle these (and they are defined in the JLS). And... they are a little more complex than that. Final variables (or static final variables) are not necessary compile time constants.

Henry

 
Henry Wong
author
Marshal
Pie
Posts: 21420
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

BTW, I wrote a quick writeup about compile time constants, a while back, that may help...

http://www.coderanch.com/t/454384/java/java/compile-time-constant

Henry
 
Sahil Kapoor
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually i was asking something different, But fortunately got an Important information about Compile time constants. Especially the one written by Henry Wong.
Special thanks to Henry Wong for such a stupendous and prolific topic.

Okay back to original doubt which still persists.....

Problem:- I was asking essentially what is the difference between static final variable and final variable as far as word "Constants" is concerned.
Final is also a Constant and Static final are also constants. then
Why if someone ask from a JAVA programmer that what is constants in java, he /she immediately replies with the the answer "static final", why not just "final".


I hope i am clear about what i am asking.

Thanks !!!
 
Michael Angstadt
Ranch Hand
Posts: 277
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sahil Rally wrote:Why if someone ask from a JAVA programmer that what is constants in java, he /she immediately replies with the the answer "static final", why not just "final".


Because when people talk about constants, they typically mean "a value that will never change throughout the entire lifetime of the application". And this is what "static final" fields are good for.
 
Jd Sierra
Greenhorn
Posts: 17
Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why it is good to treat constants as static and final inspite of only final?

When you want to declare constant, you mean that its value cannot be changed later on. This can be done very easily by
using final access modifier. That's good.

But once you are declaring variable as final, its value is never going to be changed. Every Object has to play with the value with which final variable is declared once. So it's very clear that when we create object, though each and every new object will get their own copy of final variable, they have nothing to do with that final variable except using it..I mean it can't be changed. So, why not to make it static and save memory? after all, each and every object is sharing same value. By making it static, we can save memory by just sharing one copy between all objects as well as we can have more clear logic. Moreover, even though variable is decalred static, we can use it by reference variable followed by dot operator. Compiler will automaticlly replace reference variable with the name of the class in which static variable lies. On the whole, there is nothing to loose by making constant static. In fact we are gaining something.

I hope I have cleared now why constants are static also with final. If I am wrong somewhere or anybody is not able to understand any of my point, feel free to reply.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic