• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

final variables

 
Buddhi Vikasitha
Greenhorn
Posts: 17
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
we can't convert int into byte without casting. This shows up a compile error..


But why is this working?? No compile errors. Prints 101.

 
Buddhi Vikasitha
Greenhorn
Posts: 17
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why is this happening when we use separate lines to declare and assign a value to the final variable, when we do both in the same line this error doesn't show up.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Buddhi Vikasitha wrote:why is this happening when we use separate lines to declare and assign a value to the final variable, when we do both in the same line this error doesn't show up.


Because the rules of the JLS state that this is a compile-time constant:


but this isn't:


It makes sense to do it that way, since there's a clear line between initializing at declaration and not doing so. If the second one counted as a constant, then it gets fuzzier and more complicated to define what is and is not a compile-time constant, in terms of what intervening code is allowed, and how to determine the constant-ness of the variable.
 
Buddhi Vikasitha
Greenhorn
Posts: 17
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:

It makes sense to do it that way, since there's a clear line between initializing at declaration and not doing so. If the second one counted as a constant, then it gets fuzzier and more complicated to define what is and is not a compile-time constant, in terms of what intervening code is allowed, and how to determine the constant-ness of the variable.


Thanks, think i got it.!.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic