1

I'm trying to get the best way to implement a file with all my static enums, without using any getters and setters, just static info, I achieve this in PHP like in the example below, do you really need getters and setters in java?

final class EnumTrade {
    const buy = 1;
    const sell = 2;
}

final class EnumGender {
    const male = 1;
    const female = 2;
}

final class EnumHttpMethod {
    const get = 1;
    const post = 2;
}
2

5 Answers 5

7
public enum EnumTrade {
  BUY, SELL
}

and so on.

Edit: If the number matters, do:

public enum EnumTrade {
  BUY(1), SELL(2)
}
Sign up to request clarification or add additional context in comments.

4 Comments

What happens if I want the integer value, as in BUY = 1
Why do you want this? The name matters, not the number.
In this case its the number that really matters
Unfortunately the database is set up in such a way that its integer values, so this is why it has to be integer
2

in java enum not necessary to have getter and setter these are used for normal POJO or beans

sample enum can be:

public enum EventRecurringType {

    YEARLY("1"),
    QUARTERLY("2"),
    MONTHLY("3"),
    WEEKLY("4"),
    DAILY("5"),
    NONE("0");


    private String value;

    EventRecurringType(String value) {
        this.value = value;
    }

    public String getValue() {
        return value;
    }

    @Override
    public String toString() {
        return this.getValue();
    }

    public static EventRecurringType getEnum(String value) {
        if(value == null)
            throw new IllegalArgumentException();
        for(EventRecurringType v : values())
            if(value.equalsIgnoreCase(v.getValue())) return v;
        throw new IllegalArgumentException();
    }
}

Comments

0
public enum EnumTrade
{
    BUY,
    SELL,
}

If all you need is the ordinal values of the enums, you can access those directly via EnumTrade.BUY.ordinal

If you want to store other data in the enum, do something like this (expanding as needed):

public enum EnumGender
{
    MALE(1),
    FEMALE(2);

    private final int value;

    private EnumGender(String value)
    {
        this.value = value;
    }

    public int getValue()
    {
        return this.value;
    }

    //In case you need to grab an enum by the value constant
    public getEnumGender(int value)
    {
        switch(value)
        {
            case 1:
                return EnumGender.MALE;
            case 2:
            default:
                return EnumGender.FEMALE;
        }
    }
}

2 Comments

Since the value here is final, there's no need for a getter - the object is immutable. It's perfectly safe to make it public.
@SteveB. True in most cases, but if you happen to be wanting to use the value in something that expects the getter (EL tags on JSPs for instance) you'd still need it
0

For sake of completeness and the fact answers pop in while writing i changed my original answer to mention you could store all your enums in one java class.

=> It is way better to have them stored in own files like user Tichodroma suggests

However translating your exmaple code you could build this in Java:

public class MyEnums {

 public enum EnumTrade{
    BUY, SELL
 }

 public enum EnumGender{
    MALE, FEMALE
 }

 public enum EnumHttpMethod{
    GET, POST
 }

}

And then use the different enums from outside like this:

MyEnums.EnumTrade.BUY

Comments

0
    public enum MyConst{ BUY(1), SELL(2), MALE(3), FEMALE(4), GET(5), POST(6);

       public final int value;

           MyConst(int value) {
               this.value = value;
           }

           public int getValue() {
               return value;
           }                                                   
    };

or Just go with

public enum MyConst{ BUY(1), SELL(2) }; and same for MALE, FEMALE .... 

Comments

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.