marcos guerra marcos guerra - 5 months ago 10
Java Question

Java Value of Field Not Used

I have the class

ModelPlayer
, the int fields
mainAction
and
quickAction
and the method
quickAction()
, in the method basically i set the quickAction value and from that it executes one of 4 methods. But i get the warning that i'm not using
quickAction
and i wanted to know why, because one of the 4 methods can only be executed once i set the value of
quickAction
, so i don't understant why it says that i'm not using it.
(NOTE: the methods
setQuickAction()
and
getQuickAction()
shown in the method belong to the class
Player
, so they're not related)

public class ModelPlayer {
private int mainAction;
private int quickAction;

public void quickAction(Player player,int quickAction){
this.quickAction=quickAction;
if (player.getQuickAction()>0){
switch(quickAction){
case 1:
engageAssistant(player);
player.setQuickAction(0);
case 2:
changeBusinessPermitTile(player);
player.setQuickAction(0);
case 3:
electCouncillorWithAssistant(player);
player.setQuickAction(0);
case 4:
mainAction(player,mainAction);
player.setQuickAction(0);
}
}


}

}

Answer

You are not using this.quickAction, but the parameter passed in input.

So you just assign a value to this.quickAction but you are not using it.

Try to substitute switch(quickAction) with switch(this.quickAction), the warning should go away.

Also you should use break after any case, see https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

Comments