Reputation: 402
I have a simple switch case
case.
public static int setMapTile() {
int a = getArmadaLength(); // 4 to 6
int b;
switch (a) {
case 4:
System.out.println(" recommended MapSize : 10 x 10");
b = setSize();// method for bigger map, return int
break;
case 5:
System.out.println(" recommended MapSize : 11 x 11");
b = setSize();// method for bigger map, return int
break;
case 6:
System.out.println(" recommended MapSize : 12 x 12");
b = setSize();// method for bigger map, return int
break;
default:
System.out.println("wrong"); // even though it is impossible!
break;
}
return b;
}
It says that b
might not have been initialized. Do I have to use setter
& getter
to assign the chosen value to b
?
Upvotes: 0
Views: 1072
Reputation: 9326
Either change this line: int b;
to int b = 0;
Or change your code to this shorter variant:
public static int setMapTile() {
int a = getArmadaLength(); // 4 to 6
switch (a) {
case 4:
System.out.println(" recommended MapSize : 10 x 10");
return setSize();// method for bigger map, return int
case 5:
System.out.println(" recommended MapSize : 11 x 11");
return setSize();// method for bigger map, return int
case 6:
System.out.println(" recommended MapSize : 12 x 12");
return setSize();// method for bigger map, return int
default:
System.out.println("wrong"); // even though it is impossible!
}
return 0;
}
Upvotes: 0
Reputation: 198314
At the point of return b
, b
should be initialised. It will be initialised in case a
is 4
, 5
and 6
; but what if a
is 28
? You're saying it shouldn't be able to happen, but things that shouldn't happen happen all the time, and the compiler likes to have all its bases covered.
Either initialise b
at top to cover all cases, or make sure to assign it in each branch of the switch
(including default
).
Another possibility (which I actually like best) is to make sure your code can not reach return
in the default case; you can throw an exception there (maybe IllegalStateException
, "there is something wrong with the code!") and the compiler will then know it can't reach return
through default
, and not complain about b
being unassigned. Your code should not happily proceed when your assumptions are violated, so this fits well.
public static int setMapTile() {
int a = getArmadaLength(); // 4 to 6
int b;
switch (a) {
case 4:
System.out.println(" recommended MapSize : 10 x 10");
b = setSize();// method for bigger map, return int
break;
case 5:
System.out.println(" recommended MapSize : 11 x 11");
b = setSize();// method for bigger map, return int
break;
case 6:
System.out.println(" recommended MapSize : 12 x 12");
b = setSize();// method for bigger map, return int
break;
default:
throw new IllegalStateException("Armada length is " + a + "?!?");
break;
}
return b;
}
In this specific case, actually, you can even factor out the b
outside of the switch
and completely avoid the problem. Also, that last break
is redundant.
public static int setMapTile() {
int a = getArmadaLength(); // 4 to 6
switch (a) {
case 4:
System.out.println(" recommended MapSize : 10 x 10");
break;
case 5:
System.out.println(" recommended MapSize : 11 x 11");
break;
case 6:
System.out.println(" recommended MapSize : 12 x 12");
break;
default:
throw new IllegalStateException("Armada length is " + a + "?!?");
}
return setSize();
}
Upvotes: 0
Reputation: 3556
There is chance that non of your cases in the switch
gets executed and if that is the case, it ll come to the default
block. In the default
block, you are not setting the value of b
. You never initialized b
in the first place. That is why this is happening.
To avoid this situation, you can do one of the followings :
int b = -1; // Initialize b
2.
default: {
System.out.println("wrong"); // even though it is impossible!
b = -1;
break;
}
Upvotes: 0
Reputation: 3688
The Java compiler doesn't know that a can only be 4 to 6, so it considers the possibility of the default case. So if the default case of the switch is taken, then, b was never initialized when you do return b
.
Upvotes: 0
Reputation: 8202
You need to assign a value to b
, when you declare it or in the default
block.
Instance fields have implicit default values when declared, but variables in methods do not.
public class Foo {
private int age; // this defaults to 0
public void bar() {
int height; // this won't compile unless you follow it with an assignment
}
}
Upvotes: 0
Reputation: 9427
The JVM will look at all possible outcomes. One of them is:
default:
System.out.println("wrong"); // even though it is impossible!
break;
after which, you return b. But, since b is a local variable, it has no default value. You'll need to initialize it for all the possibilities, including the default one:
default:
System.out.println("wrong"); // even though it is impossible!
b = 0;
break;
Or give b a value on declaration:
int b = 0;
Upvotes: 0
Reputation: 1679
As the exception states, you never initialize b by assigning it a value if your switch should run its default
case.
int b;
merely declares it.
to fix your issue, you can simply change it to int b = 0;
Upvotes: 0
Reputation: 69440
No you do not need a setter. Simply initialize the variable: int b = 0;
Upvotes: 2
Reputation: 2075
Yes you didn't initialized b
. Set some default value as per your requirement, say, int b=-1;
Upvotes: 0