• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need Help on variable initialization  RSS feed

 
Wee Keong Soh
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
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • 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: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!