Why can't I define a static method in a Java interface?

后端 未结 24 1103
没有蜡笔的小新
没有蜡笔的小新 2020-11-22 05:44

EDIT: As of Java 8, static methods are now allowed in interfaces.

Here\'s the example:

public interface IXMLizable         


        
24条回答
  •  感情败类
    2020-11-22 06:40

    While I realize that Java 8 resolves this issue, I thought I'd chime in with a scenario I am currently working on (locked into using Java 7) where being able to specify static methods in an interface would be helpful.

    I have several enum definitions where I've defined "id" and "displayName" fields along with helper methods evaluating the values for various reasons. Implementing an interface allows me to ensure that the getter methods are in place but not the static helper methods. Being an enum, there really isn't a clean way to offload the helper methods into an inherited abstract class or something of the like so the methods have to be defined in the enum itself. Also because it is an enum, you wouldn't ever be able to actually pass it as an instanced object and treat it as the interface type, but being able to require the existence of the static helper methods through an interface is what I like about it being supported in Java 8.

    Here's code illustrating my point.

    Interface definition:

    public interface IGenericEnum > {
        String getId();
        String getDisplayName();
        //If I was using Java 8 static helper methods would go here
    }
    

    Example of one enum definition:

    public enum ExecutionModeType implements IGenericEnum {
        STANDARD ("Standard", "Standard Mode"),
        DEBUG ("Debug", "Debug Mode");
    
        String id;
        String displayName;
    
        //Getter methods
        public String getId() {
            return id;
        }
    
        public String getDisplayName() {
            return displayName;
        }
    
        //Constructor
        private ExecutionModeType(String id, String displayName) {
            this.id = id;
            this.displayName = displayName;
        }
    
        //Helper methods - not enforced by Interface
        public static boolean isValidId(String id) {
            return GenericEnumUtility.isValidId(ExecutionModeType.class, id);
        }
    
        public static String printIdOptions(String delimiter){
            return GenericEnumUtility.printIdOptions(ExecutionModeType.class, delimiter);
        }
    
        public static String[] getIdArray(){
            return GenericEnumUtility.getIdArray(ExecutionModeType.class);
        }
    
        public static ExecutionModeType getById(String id) throws NoSuchObjectException {
            return GenericEnumUtility.getById(ExecutionModeType.class, id);
        }
    }
    

    Generic enum utility definition:

    public class GenericEnumUtility {
        public static  & IGenericEnum> boolean isValidId(Class enumType, String id) {       
            for(IGenericEnum enumOption : enumType.getEnumConstants()) {
                if(enumOption.getId().equals(id)) {
                    return true;
                }
            }
    
            return false;
        }
    
        public static  & IGenericEnum> String printIdOptions(Class enumType, String delimiter){
            String ret = "";
            delimiter = delimiter == null ? " " : delimiter;
    
            int i = 0;
            for(IGenericEnum enumOption : enumType.getEnumConstants()) {
                if(i == 0) {
                    ret = enumOption.getId();
                } else {
                    ret += delimiter + enumOption.getId();
                }           
                i++;
            }
    
            return ret;
        }
    
        public static  & IGenericEnum> String[] getIdArray(Class enumType){
            List idValues = new ArrayList();
    
            for(IGenericEnum enumOption : enumType.getEnumConstants()) {
                idValues.add(enumOption.getId());
            }
    
            return idValues.toArray(new String[idValues.size()]);
        }
    
        @SuppressWarnings("unchecked")
        public static  & IGenericEnum> T getById(Class enumType, String id) throws NoSuchObjectException {
            id = id == null ? "" : id;
            for(IGenericEnum enumOption : enumType.getEnumConstants()) {
                if(id.equals(enumOption.getId())) {
                    return (T)enumOption;
                }
            }
    
            throw new NoSuchObjectException(String.format("ERROR: \"%s\" is not a valid ID. Valid IDs are: %s.", id, printIdOptions(enumType, " , ")));
        }
    }
    

提交回复
热议问题