You cannot return an instance when your method is static, but you can make your enum interface implemented and create an instance method that invokes the static method, and does a virtual dispatch for you:
public interface EncryptionAlgo { String hash(final String text, final byte[] sb); } public enum Algorithm implements EncryptionAlgo { SHA1("sha1") { public String hash(final String text, final byte[] sb) { return Sha1.hash(text, sb); } }, HMAC("hmac") { public String hash(final String text, final byte[] sb) { return Hmac.hash(text, sb); } }; Algorithm(final String algorithm) { this.algorithm = algorithm; } private final String algorithm; public String getAlgorithm() { return algorithm; } }
Now you can call hash on the SHA1 or HMAC instance, for example:
Algorithm.HMAC.hash(someText, sb);
or bypass EncryptionAlgo instances, for example:
EncryptionAlgo algo = Algorithm.SHA1;
source share