• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Need Help on variable initialization

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


For the above code segment, I have not initialized the variable thePrice. The if statement will always be true here in this case and thePrice will be initialized to be weight*10. However, this would not work unless i take away the if statement. Can anyone tell me why?

Thanks
 
Marshal
Posts: 64647
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because the compiler cannot verify that you cannot change weight before the "if". Try changing "int weight" to "final int weight", then the compiler can verify that weight cannot change.
 
Wee Keong Soh
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a million! This is crystal clear now..
 
Campbell Ritchie
Marshal
Posts: 64647
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
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!