sriram
sriram

Reputation: 9062

How do I use Enum in a proper way?

I'm very to Java and I use enum for the first time like this:

    public class Web {
        public String baseUrl;
        public static enum Environment {

            DEVELOPMENT("http://development") ,
            PRODUCTION("http://production"),
            SANDBOX("http://sandbox");

            public final String baseUrl;

            private Environment(String baseUrl)
            {
                this.baseUrl = baseUrl;
            }
        }
    }

The enum Environment has the three constants DEVELOPMENT, PRODUCTION,SANDBOX. The Web class also has the baseUrl to which the Environment's baseUrl to be set ( not sure this is the good practice to do so ).

For setting up the baseUrl I'm currently doing like this :

new Web().setBaseUrl(Web.Environment.PRODUCTION.baseUrl)

I'm not sure this is the right way to use the enums with the classes. Are there any way to directly set Web baseUrl to enums baseUrl.

Am I missing something here.

Thanks in advance

Upvotes: 3

Views: 1214

Answers (5)

Marvo
Marvo

Reputation: 18143

I think you're on the right track, but you're losing some of the strength of Java's enum by accessing the URL in this manner. Instead, use the type of the enum to help ensure that you pass correct values to your method. That is, pass the enum alone, and let the method extract whatever value from it. For example:

new Web().setEnvironment(Web.Environment.PRODUCTION);

Now you can only pass Environments to your Web class, rather than any ol' string.

[Edit] Then your Web.setEnvironment method looks like this:

public void setEnvironment(Environment environment) {
    this.baseUrl = environment.getBaseUrl();
}

This way, I can't come along and call new Web().setEnvironment("marvo") by accident. It enforces a certain level of correctness.

And vishal_aim is right. Even with Enums you should practice data hiding and encapsulation, so make the instance variable private, and provide an accessor like getBaseUrl() to retrieve the value.

Upvotes: 3

Torben
Torben

Reputation: 3911

Along with what Bohemian and vishal_aim said, you might be unnecessarily exposing the baseUrl member (or getBaseUrl() method after you refactor it in). Instead pass the enum to the Web class and have it dig the URL from the enum. That way, if you keep the enum inside the Web class, you can keep the baseUrl private.

public void setEnvironment(Environment env) {
    baseUrl = env.getBaseUrl();
    ....
}

Upvotes: 0

aviad
aviad

Reputation: 8278

I think there is nothing wrong with how you use Enum. However the code looks little clumsy:

new Web().setBaseUrl(Web.Environment.PRODUCTION.baseUrl)

I would take the Enum out of the Web class and omit the redundant 'baseUrl' field in the Web class- you do not need it since you have the value in your Enum. Instead of the baseUrl I would have a urlType member of type Environment. Also would be nice to add accessor method to the baseUrl inside the Environment enum

Upvotes: 1

Bohemian
Bohemian

Reputation: 425458

Make the enum a top level class/enum.
Add a getter for the baseUrl field.

Upvotes: 1

vishal_aim
vishal_aim

Reputation: 7854

Rather than using directly Enum's member baseUrl you should make it private and have a getter method in enum class. other than that I think it's ok to use enum this way

Upvotes: 2

Related Questions