aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes what is the difference between final and compile constant? 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 "what is the difference between final and compile constant? " Watch "what is the difference between final and compile constant? " New topic
Author

what is the difference between final and compile constant?

Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531
this is from k&B from the switch chapter..
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
see if this helps you.


SCJP 1.6 96%
Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531
but here i=90; how does this mean that the value is being set at runtime and not at compile time? isnt i final here?
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
i is final, when its assigned a value it will not be changed.

I dont know if I am right, but what I think is that if assignment is done with declaration compiler does the assignment and replaces the final variable with its value.
but if final variable is declared and assignment part is done somewhere else compiler just checks the compatibility between the variable type and the value assigned, and actual assignment is done at runtime.

For Example:

here when compiler encounters line 1 and if it assigns value then what will happen when it calls the constructor at line 2.

so if final variable is not assigned a value at declaration time, the assignment will be done at runtime according to the code.

correct me if I am wrong.
Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531
this makes a lot of sense.....thanks neha
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
you are always welcome.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: what is the difference between final and compile constant?