aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Static Final Variables Query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Static Final Variables Query" Watch "Static Final Variables Query" New topic
Author

Static Final Variables Query

Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
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 !!!

SCJP 6.0 96%

(Connecting the Dots ....)
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

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??


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 440

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?

SCJP 6 [86%] June 30th, 2010
OCPWCD [84%] March 26th, 2012
OCEJPAD [83%] September 6th, 2014
If you find any post useful, click the "plus one" sign on the right
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18847
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18847
    
  40


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

Joined: Sep 12, 2009
Posts: 316
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

Joined: Jun 17, 2009
Posts: 273

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.


SCJP 6 || SCWCD 5
Jd Sierra
Greenhorn

Joined: Jul 09, 2011
Posts: 17

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Static Final Variables Query