简体   繁体   中英

The method createOperate cannot be declared static; static methods can only be declared in a static or top level type

public class Welcome {
    public class OperationFactory {

        public static Operation createOperate(String operate) {
            Operation operation = null;
            if("+".equalsIgnoreCase(operate)) {
                operation = new OperationAdd();
            }else if("-".equalsIgnoreCase(operate)) {
                operation = new OperationSub();
            }else if("*".equalsIgnoreCase(operate)) {
                operation = new OperationMul();
            }else {
                operation = new OperationDiv();
            }
            return operation;
        }
    }
}

The question is that:The method createOperate cannot be declared static; static methods can only be declared in a static or top level type I do not know how to resolve. Thank you

As the error message clearly states, you have 3 options :

  1. change the createOperate method to be non-static
  2. change the OperationFactory class to be static
  3. move the static method createOperate to the enclosing Welcome class

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