2

Hi guys this is my first post so please forgive me if i make any mistakes. Here is my problem:

public class Main {
public static void main(String[] args) {
    int onlineplayer = 0;
    int maxplayer;
    switch(onlineplayer) {
        case 0:
        maxplayer = 1;
        break;
        case 1:
        maxplayer = 2;
        break;
    }
    System.out.print(onlineplayer + "/" + maxplayer);
}

This line of code returns this error:

Main.java:13: error: variable maxplayer might not have been initialized
    System.out.print(onlineplayer + "/" + maxplayer);
                                          ^

While

public class Main {
public static void main(String[] args) {
    int onlineplayer = 0;
    int maxplayer;
    switch(onlineplayer) {
        case 0:
        maxplayer = 1;
        System.out.print(onlineplayer + "/" + maxplayer);
        break;
        case 1:
        maxplayer = 2;
        System.out.print(onlineplayer + "/" + maxplayer);
        break;
    }
}

This one works. I stated learning Java a while ago and I think they are both the same thing. Can someone please explain to me what is the difference and why doesn't the first one work?

2 Answers2

3

The code might look similar but the difference is the following:

In your second example the print function is called directly after setting maxplayer, so at the time of reading from the variable, it is guaranteed to be set.

But in your first example the switch is not guaranteed to enter any of the cases, so when reaching the print line, maxplayer could still be uninitialized.

To prevent this you usually add a default case which is executed if no other case matches. That way it is guaranteed that maxplayer is set:

switch(onlineplayer) {
    case 0:
        maxplayer = 1;
        break;
    case 1:
        maxplayer = 2;
        break;
    default:
        System.out.print("Some warning");
        maxplayer = 1; // some default value
}

Remember, you should always have a default case in your switch! See this good explanation of why the default case is important: https://stackoverflow.com/a/5241196/1174343

Community
  • 1
  • 1
x squared
  • 3,173
  • 1
  • 26
  • 41
  • Note if you think that `default` branch is impossible, you may just write `default: throw new AssertionError();` without setting any value to `maxplayer`. – Tagir Valeev Aug 15 '15 at 11:10
  • You mention an important point! Nobody should just take this code snippet and follow the practice in it bindly. It always depends on the application how to handle the `default` case. I guess, a good start is always to ask oneself how bad it is for the program if you land in the default case. – x squared Aug 15 '15 at 11:13
2

Because the compiller is not "smart" enough.

In your first snipset of code if onlineplayer has a value different than 0 or 1 then maxplayer will remain uninitialized.
Of course that is not actually possible since onlineplayer is initialized with value 0 and no other thread may modify it. But the compiler is not able to recognize that.

This lack of "smartness" is actually as per dessign. The Java Language Specification has rules for definite assignment which you can read at chapter 16

Anonymous Coward
  • 3,140
  • 22
  • 39