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?
posted 8 years ago
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.