Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what is the difference between final and compile constant?

 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is from k&B from the switch chapter..
 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
see if this helps you.
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this makes a lot of sense.....thanks neha
 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you are always welcome.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic