Why is initializing a string in an if statement seem different than in a switch statement?

  • A+

I'm learning Java and I'm making simple programs to find the season that a month is in, based off some book examples. These two classes demonstrate two ways of testing a value: if/else if statement, and switch statement. The thing i'm confused with is the string that is used to hold the season. When I declare it as just String season; it works with the if statements. But with the switch statement, doing that produces a "The local variable season may not have been initialized" error.

public class IfElse {     public static void main(String args[]) {         int month = 5;         String season;             // isn't initialized, works fine          if(month == 12 || month == 1 || month == 2)             season = "Winter";         else if(month == 3 || month == 4 || month == 5)             season = "Spring";         else if(month == 6 || month == 7 || month == 8)             season = "Summer";         else             season = "Fall";          // this is okay         System.out.println("May is a " + season + " month.");     } } 

Not initializing season at the same time as declaration works fine for the above code, but the season variable in the last println() for the switch produces an error if it's declared the same way.

The following code doesn't work:

public class Switch {     public static void main(String args[]) {         int month = 5;         String season;             // HAS to be initialized, currently causes error         switch(month) {         case(12):         case(1):         case(2):              season = "Winter";             break;         case(3):         case(4):         case(5):              season = "Spring";             break;         case(6):         case(7):         case(8):              season = "Summer";             break;         case(9):         case(10):         case(11):              season = "Fall";             break;          default:             System.out.println("Invalid month");             break;         }         System.out.println("May is a " + season + " month");     }             // produces an error if season isn't initialized to null or "" } 

What causes this? Is it the braces enclosing the switch statement, or a problem with the switch statement itself? How is initializing a string inside an if statement any different than initializing it inside a switch statement? I just can't seem to understand this.

Sorry if this is extremely obvious or if it seems like a dumb question.


That is because you did not specify what season has to be in the default case. What happens when month is not within 1-12? season will not be initialized.

if you are expecting strictly only 1-12 as month input, then you might want to consider throwing an Exception in default:

default:     throw new IllegalArgumentException("Invalid month"); 


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: