Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Final Variables  RSS feed

 
Ranji Sura
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

When compiling this code , there is no error... but when I change this code to like this.... >>>

There is a error now, why is this happened ? Why compiler doesn't reorganize the final variable value when we initialize it later....?
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ranjith Suranga wrote:
When compiling this code , there is no error... but when I change this code to like this.... >>>

There is a error now, why is this happened ? Why compiler doesn't reorganize the final variable value when we initialize it later....?



Wow. These topics seems to come in batches. I would be interested to know the relationship ... probably related to taking the same class I reckon.

Two topics from yesterday...

http://www.coderanch.com/t/596232/java/java/Why-there-compiler-error
http://www.coderanch.com/t/596251/java/java/Final-variables-initializing

Henry
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ranjith Suranga wrote:
There is a error now, why is this happened ? Why compiler doesn't reorganize the final variable value when we initialize it later....?



BTW, the other two topics do answer this question, but to save time. The compile error is not directly related to final variables -- it is related to compile time constant variables, and that is related to final variables. See this link for more details.

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

So, basically, in one case, it is a compile time constant. In the other, it is not a compile time constant.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!