简体   繁体   中英

How to return .class from Java method?

From a utility method in java I intend to return .class, but I am not sure how to achieve it. When I try with below method, It gives compilation error. Please suggest what is the correct approach ?

T getProviderClass(String customerType){

        switch (customerType){
            case "FACEBOOK":{
                return FacebookApi.class;
            }
            case "GOOGLE":{
                return GoogleApi20.class;
            }
            default:return null;
        }
    }

Use java.lang.Class as the return type.

Class<?> getProviderClass(String customerType){
}

Instead of using T you should do it like this:

Class<?> getProviderClass(String customerType) {
     switch (customerType) {
          case "FACEBOOK":{
               return FacebookApi.class;
          }
          case "GOOGLE":{
               return GoogleApi20.class;
          }
          default: return null;
      }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM