Tristan Morgan
Tristan Morgan

Reputation: 35

Switch on enum in Java does not go to correct case

I'm doing some coding for Android in Android Studio, and I have a class TaskType with a private enum in it, which I pull out with a getTaskType, as in the code below:

public class TaskType {
    public enum taskType {NOT_SET, LOGIN_TASK, GET_USER_DETAILS_TASK}
    private taskType theTaskType= taskType.NOT_SET;

    public TaskType(taskType taskType){
        this.theTaskType= taskType;
    }

    public taskType getTaskType() {
        return theTaskType;
    }

    public void setTaskType(taskType newTaskType) {
        theTaskType = newTaskType;
    }
}

I'm then passing an instance into a callback as an argument and trying to switch on it, as in the code below:

public void callbackOnPostExecute(TaskType taskType){
    switch(taskType.getTaskType()){
        case LOGIN_TASK:
            mListener.callback(loginNetworkHelper);
        default:
            Log.e(TAG, "Unknown task type: " + taskType.getTaskType());
            error.setError(ErrorHelper.error.UNKNOWN_ERROR);
            mListener.callback(new NetworkHelper(error));
    }
}

This seems to compile just fine, and Android Studio even highlights LOGIN_TASK purple as though it knows what it is. However, the switch goes straight for the default case, and my Log.e statement has the output:

03-24 07:23:43.870    2636-2636/com.mydomain.myproject E/NetworkInterface﹕ Unknown task type: LOGIN_TASK

This makes it look as though the correct enum value is being used, but is for some reason not recognised by the LOGIN_TASK case.

I've done a lot of searching on this issue, but all I can pull up is tutorials on how to switch on a enum in general in Java, which I think I've followed. Does anyone have any input?

Thanks!

Upvotes: 2

Views: 882

Answers (2)

aga
aga

Reputation: 29416

You forgot to add break statement in your case clause:

case LOGIN_TASK:
    mListener.callback(loginNetworkHelper);
    break;

default:
    Log.e(TAG, "Unknown task type: " + taskType.getTaskType());
    error.setError(ErrorHelper.error.UNKNOWN_ERROR);
    mListener.callback(new NetworkHelper(error));

If you don't end the case with the break switch will call all case branches including the default branch. Documentation on switch statement can be found here.

Upvotes: 5

A N M Bazlur Rahman
A N M Bazlur Rahman

Reputation: 2300

You need to add a break statement, otherwise it will always end up with the default case.

public void callbackOnPostExecute(TaskType taskType){
    switch(taskType.getTaskType()){
        case LOGIN_TASK:
            mListener.callback(loginNetworkHelper);
            break; // put a break here, and do this for every case clause other than default one. 
        default:
            Log.e(TAG, "Unknown task type: " + taskType.getTaskType());
            error.setError(ErrorHelper.error.UNKNOWN_ERROR);
            mListener.callback(new NetworkHelper(error));
    }
} 

Upvotes: 0

Related Questions