CrypticCabub CrypticCabub - 1 year ago 76
Java Question

Polymorphic construction from String identifier

I'm trying to setup a polymorphic ExportRule system for my program, but I'm having trouble figuring out how to construct the desired ExportRule from a config file which has the ExportRule defined at the top of the file via a String.

I was looking to do something like this:

private ExportRule loadExportRule(String fileName) throws IOException, InvalidConfigurationException, FileNotFoundException{
if(fileName == null) {
throw new NullPointerException("fileName cannot be null!");
YamlConfiguration exportRuleConfig = new YamlConfiguration();

return ExportRules.fromString(exportRuleConfig.getString("exportRule")).fromYamlConfiguration(exportRuleConfig);

where ExportRules is an enum mapping a String to the actual .class (which extends ExportRule) and then would call a static fromFileConfiguration() factory method to generate the ExportRule from the file which looks like this:

exportRule: nameOfExportRule
//all the configurationstuff for the rule

The problem I'm having is how to actually call the fromFileConfiguration() method as it is a static method and therefore java will not allow me to guarantee it's existence via the ExportRule interface

Is there a clean way to achieve something like this or do I have to do some kind of switch statement in my enum for each ExportRule?


Thank you glee8e!

here's my enum:

public enum ExportRules {
COMMENT_CONCAT("CommentConcat") {
public ExportRule fromFileConfiguration(FileConfiguration fileConfiguration) throws InvalidConfigurationException {
return CommentConcat.fromFileConfiguration(fileConfiguration);

private String name;

ExportRules(String name) { = name;

public abstract ExportRule fromFileConfiguration(FileConfiguration fileConfiguration) throws InvalidConfigurationException;

public String getName() {
return name;

public static ExportRules fromString(String str) {
for(ExportRules rule : ExportRules.values()) {
if(str.equalsIgnoreCase(rule.getName())) return rule;
return null;


Answer Source

Polymorphism is not for static stuff but instance methods, at least in Java. Make your fromFileConfiguration an abstract instance method, and implememt it in individual constants:

public enum ExportRules {
    A_RULE {
         @Override public ExportRule fromFileConfiguration(YamlConfiguration c) {...}
    public abstract ExportRule fromFileConfiguration(YamlConfiguration c);


Remember java enums are also normal classes. They can implement interfaces, have abstract methods and so on. You can't have it a non-Enum superclass and a non-private constructor though.

P.S.: For Java8, since this enum has one single abstract method, you can use a Function instead of making a lot of sub-classes, like this:

public enum ExportRules {
    A_RULE((c) -> {//...}),
    private final Function<YamlConfiguration, ExportRule> func;
    private ExportRules( Function<YamlConfiguration, ExportRule> fun) {
        func = fun;

    public ExportRule fromFileConfiguration(YamlConfiguration c) {
        return func.apply(c);


This is not classic polymorphism but it does the job and reduces scraps generated.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download