2

I'm novice in learning java, in this example that try to simulate roll dice game.

Compiler error appears when I do not initialize myPoint variable, so I set it to zero before switch construct.

I want to understand why do I need to initialize myPoint variable when it's value is set to value of sumOfDice within switch after it is declared, Unlike gameStatus variable?

  /***** roll dice game ****/

 //some code here..

   private enum Status { CONTINUE, WON, LOST };

   // plays one game of craps
   public static void main( String[] args )
   {
      // point if no win or loss on first roll
      int myPoint = 0; 
      Status gameStatus; // can contain CONTINUE, WON or LOST

      int sumOfDice = rollDice(); // first roll of the dice

      // determine game status and point based on first roll 
      switch ( sumOfDice ) 
      {
         case SEVEN: // win with 7 on first roll
         case YO_LEVEN: // win with 11 on first roll           
            gameStatus = Status.WON;
            break;
         case SNAKE_EYES: // lose with 2 on first roll
         case TREY: // lose with 3 on first roll
         case BOX_CARS: // lose with 12 on first roll
            gameStatus = Status.LOST;
            break;
         default: // did not win or lose, so remember point         
            gameStatus = Status.CONTINUE; // game is not over
            myPoint = sumOfDice; // remember the point
            System.out.printf( "Point is %d\n", myPoint );
            break; // optional at end of switch
      } // end switch 

// rest of the code here ..

      // display won or lost message

   // roll dice, calculate sum and display results
   public static int rollDice()
   {
       // return sum of dice
    } 
} // end class Craps
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • 2
    http://stackoverflow.com/questions/1560685/why-must-local-variables-including-primitives-always-be-initialized-in-java – Darshan Mehta Mar 02 '16 at 12:54
  • The code you provided would not fail if myPoint is not initialized. You probably use the myPoint variable somewhere outside the `default` block, where it might be uninitialized. – pkalinow Mar 02 '16 at 13:15

2 Answers2

2

According to Java specification

A local variable (§14.4, §14.14) must be explicitly given a value before it is used, by either initialization (§14.4) or assignment (§15.26), in a way that can be verified using the rules for definite assignment (§16).

The rules for switch statement effectively enforce you to initialize your variable in every possible execution path for the switch statement.

So the answer is:

  • gameStatus variable is initialized in every possible execution path through the switch statement, therefore compiler is satisfied.
  • myPoint would not be initialized if, e.g., sumOfDice were SEVEN, therefore the compiler forces you to initialize the variable outside the switch block
Mifeet
  • 12,949
  • 5
  • 60
  • 108
0

In your switch-case, any path will initialize the gameStatus variable.

This is not the case for myPoint , so the compiler complains about the missing initialization .

Arnaud
  • 17,229
  • 3
  • 31
  • 44