0

Hi I am in a situation where I need to use multiple else if's and I would like to use switch statements, I have tried a lot but I am not able to do so. How can I convert the following else if's to switch?

public ApiObservables(Object o) {
        if (o instanceof SitesController) {
            mSitesApi = getSitesApi();
        } else if (o instanceof QuestionController) {
            mQuestionApi = getQuestionApi();
        } //more else if's
    }

I would like to do something like this:

public ApiObservables(Object o) {
        switch (o) {

        }
    }
1
  • Usually it's better to use polymorphic method calls instead of instanceof checks, even if it could be done with a switch statement. Commented Apr 5, 2016 at 12:57

4 Answers 4

2
  • When using switch case, the control variable must be of primitive type, or String, or enum. You cannot use objects in switch-case. From JLS:

    The type of the Expression must be char, byte, short, int, Character, Byte, Short, Integer, String, or an enum type

  • The switch case only checks for equality ( i.e., similar to using == operator). So, you cannot use instanceof in switch-case

Sign up to request clarification or add additional context in comments.

4 Comments

So do you mean that it is not possible to use switch in this case?
@NonthonbamTonthoi Yes.
You can switch Enums too
@Guy Yes. Just checked the JLS
1

In your situation, I would use method overloading :

public ApiObservables foo(Object o) {
  //throw new IllegalArgumentException?
}

public ApiObservables foo(SitesController o) {
  return getSitesApi();
}

public ApiObservables foo(QuestionController o) {
  return getQuestionApi();
}

1 Comment

Thank you for your reply
1

I don't know if you can refactor your code but What about a diffetent approach using interfaces? Something like this:

interface API {
   //method signatures here
}

class SitesApi implements API {
    //implementation here
}

class QuestionApi implements API {
    //implementation here
}

interface Controller {
    API getAPI();
}

class QuestionController implements Controller {
    @Override
    public API getAPI() {
        return new QuestionAPI();
    }
}

class SitesController implements Controller {
    @Override
    public API getAPI() {
        return new SitesAPI();
    }
}

Then:

public ApiObservables(Controller controller) {
    someApi = controller.getAPI();
}

Comments

0
    I don't know about your remaining code try this by passing in this function repective controller string as per your object

    and if you use object in switch case then i think it will not support

try this one

        public ApiObservables(String o) {
            switch(o) {
            case "SitesController":
               mSitesApi = getSitesApi();
               break;

             case "QuestionController":
            mQuestionApi = getQuestionApi();

             default:
               System.out.println("place any thing that you want to show any message");
               break;
            }
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.