0

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 understand 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);
            }
        }


        }

}
MLavoie
  • 9,671
  • 41
  • 36
  • 56
  • *i get the warning that i'm not using quickAction*: what is the exact and complete warning message? Why do you use an int and then compare it to char values? Have you considered using an enum instead? Note that, if that is the only code in ModelPlayer, you indeed have a private field that is set in the method, but never read anywhere. So it is indeed useless. – JB Nizet Jun 05 '16 at 13:21
  • 1
    another remark to answers below: 1) add break; between each case unless you want them all to run. 2) quickAction is an int, and your case is 'char' so remove the ' – ItayD Jun 05 '16 at 13:31

3 Answers3

0

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

Loris Securo
  • 7,538
  • 2
  • 17
  • 28
0

I believe you get this error because you use the quickAction method parameter

public void quickAction(Player player,int quickAction)

to evaluate the switch statement, and not the private class variable, hence the private class variable

private int quickAction;
...
this.quickAction

is not used anywhere, and therefore the error.

0

The IDE tells give you the warning because the quickActionfield is never actually used.

In public void quickAction(Player player,int quickAction) method, you're never actually using the field quickAction. You're using the quickAction passed in from the method.

There is a Javabean standard that dictates how you should structure your object and fields. You don't need to follow this standard but it is widely used for defining 'properties' in your class.

All you need to know is that for every field you consider a property of the class, you have a 'getter' and a 'setter' for it. Consider the code below

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

    // make getters and setters for the fields
    public int getQuickAction { return quickAction; }
    public void setQuickAction(int quickAction) { this.quickAction = quickAction; }

    public int getMainAction { return mainAction; }
    pulbic void setMainAction(int mainAction) { this.mainAction = mainAction; }

    // remove the quickAction parameter because we can use the field 'quickAction'
    public void quickAction(Player player){
        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);
            }
        }
    }
}

This code will surely not have any errors. Eclipse and other IDEs even give you the option to generate these 'getters' and 'setters' from your fields in the class.

Community
  • 1
  • 1
Rico Kahler
  • 17,616
  • 11
  • 59
  • 85