3

I want to use an Enum in Java for storing configuration values for different environments. Each Enum will have the same fields, but different values. Something like:

public enum DevelopmentConfig
{
   URL("..."),
   defaultURL(".....");
}

public enum ProductionConfig
{
   URL("..."),
   defaultURL(".....");
}

This is for a web application, so I can't simply use Preferences or any other solution.

My question is, is there a way to create an interface to define the fields of the configuration? Or should I be using a normal class instead of enum for storing these values?

Edit: To use this, I simply want to do this from my other classes:

String url = Config.URL

Or

String url = Config.getURL();

Without knowing if that refers to Config.Development or Config.Production (I want that to be determined in the Config enum's constructor itself and have it choose the right set of fields)

3
  • Enums can implement interfaces, if that's what you're asking. Commented Feb 5, 2013 at 4:06
  • Can you, please, show us how are you going to use this? Enum constants are static. Do you want to pass a class somewhere? Commented Feb 5, 2013 at 4:10
  • @defaultlocale I've edited it to add some more details Commented Feb 5, 2013 at 4:24

3 Answers 3

9

You're misusing enums.

Each enum member can be implemented as an anonymous class that overrides things:

public enum Config {
    DEVELOPMENT {
        @Override
        ...
    },
    PRODUCTION {
        @Override 
        ...
    };

    public abstract ...;
}
Sign up to request clarification or add additional context in comments.

4 Comments

what's the use of defining everything as a class when I simply want to store one string or int value. Should I use a regular class instead?
I think a regular class would be better, as there I could also specify the data type of the fields. What would you suggest?
@ClickUpvote: You should store all of the configuration settings as methods or final fields in the enum.
@ClickUpvote: Exactly what I just wrote. Make an abstract url() method.
2

You can also use enum in the following manner. With this you can store(and retrieve) different values for each Config

    public enum Config{
        DEVELOPMENT("DEV_URL", "DEV_DEFAULT_URL"), 
        PRODUCTION("PROD_URL", "PROD_DEFAULT_URL");

    private String url;

    private String defaultURL;

    Config( String url, String defaultURL )
    {
        setUrl(url);
        setDefaultURL(defaultURL);
    }

    public String getUrl()
    {
        return url;
    }

    public String getDefaultURL()
    {
        return defaultURL;
    }

    public void setDefaultURL( String defaultURL )
    {
        this.defaultURL = defaultURL;
    }

    public void setUrl( String url )
    {
        this.url = url;
    }
}

2 Comments

That's not bad but if I have to add a 100 configuration options later, I will need to supply a 100 arguments via the constructor.
@ClickUpvote: That's why you should user overridden abstract methods instead.
1

Each member of the enum is an instance of the enum class. That means that you can define methods, variables and implement interfaces:

public interface Config {
    String getKey();
}

public enum DevelopmentConfig implements Config
{
    URL("url"),
    DEFAULT_URL("defaulturl");

    private String key;

    private DevelopmentConfig(String key){
        this.key = key;
    }

    public String getKey(){
        return this.key;
    }
}

If you're looking to use enums to look-up values, I would recommend using them as a key in a Map instead of implementing different types per need.

Edit

You can accomplish this by reading in a .property file from a location in the environment your application is running in (dev / prod / etc), then keying into the property file with the enum:

//This has reads in a property file:
PropertyManager propertyManager = new PropertyManager(/*prop file location*/);

String url = propertyManager.getConfig(DevelopmentConfig.URL);

PropertyManager's API would look like the following:

PropertyManager {
    String getConfig(Config config);
}

5 Comments

What I'm trying to do is have two sets of configuration values, both with the same fields and different values. Then from my other classes, I simply want to use Config.URL without knowing whether it refers to Config.DEV.URL or Config.PRODUCTION.URL. What would you suggest for this?
I have actually done exactly this with pairing enums with the Properties read from a .property file.
Problem with that is, I won't have autocomplete for the fields in Netbeans which is what I want. Can you tell me how can I accomplish this via enums or regular classes?
The enums, propertyManager and configurations will all autocomplete. Im not sure what you are looking for.
i think i'm quite clear in describing what I want.. I've even added some code examples in my edit to the question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.