简体   繁体   中英

Static method vs object method which is better in such code sample

public class VerifyFactory {

    private static final VerifyFactory INSTANCE = new VerifyFactory();

    private VerifyFactory() {
    }

    public static VerifyFactory getInstance() {
        return INSTANCE;
    }

    public Verifiable getVerifyForEntryConference(ServerOperations serverOperations, String logPath,
         language);
    }

    public Verifiable getVerifyForEntryTone(ServerOperations serverOperations, String logPath,


    }


    }


public class VerifyFactory {


    public static Verifiable getVerifyForEntryConference(ServerOperations serverOperations, String logPath,
         language);
    }

    public static Verifiable getVerifyForEntryTone(ServerOperations serverOperations, String logPath,


    }


    }

static method vs object method which is better in such code sample.

"static method vs object method which is better" well it depends on the purpose of your usage.

If you want your method to be called up only after an instance of the class (containing that method) has been created, then you should define that method as non-static

And if you want to call your method without creating an instance of the class, then you should define your method as static

Object method is much better than static but it depend on method and requirements. i generally used static when it is not possible to create object before method call. and if your class has some instance variable then use object methods. you can't use class instance variables in static method.

object methods are specific to objects but static methods are not.

So if your method is not interfering with instance variables then you can make them static.

you can check this Ques. also : Difference between Static methods and Instance methods

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