Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

final variables

 
Greenhorn
Posts: 17
Netbeans IDE Java 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
Netbeans IDE Java 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.
 
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
Netbeans IDE Java 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.!.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!