none none
none none

Reputation: 203

Enum static or non-static method

Consider the following enum class

public enum ClassA {
    CHECK1("X", 0),
    CHECK2("Y", 2),
    CHECK3("Z", 1);

    private final String id;
    private final String cdValue;

    private ClsA(String id, String cdValue) {
        this.id = id;
        this.cdValue = cdValue;
    }

    private String getId() {
        return id;
    }

    private String getCdValue() {
        return cdValue ;
    }

    private static final List<String> cdValues = new ArrayList<String>();

    static {
        for (ClassA clsA : ClassA.values()) {   
            cdValues.add(clsA.getCdValue());    
        }
    }   

    public boolean isCdValue(String cdValue)
    {
        if clsValues.contains(cdValue)
            return true;
        else return false;
    }   
}

The question that I have is does the method isCdValue has to be static. I have to use this method isCdValue for every input given by the client. Therefore the method parameter cdValue changes for every input.

If it cannot be static then I would like to know how I can access this method. Please note I am primarily interested in learning about static of non-static method call. If it is a non-static call in a enum then how can we call this non static method. I am not trying to resolve the issue of how to get about checking the cdValue exists or not. It is just an example.

Upvotes: 13

Views: 37832

Answers (4)

Ritesh Gune
Ritesh Gune

Reputation: 16729

does the method isCdValue has to be static.

Yes, the method isCdValue has to be static here. An enum is a special kind of class. An enum constant defines an instance of the enum type. An enum type has no instances other than those defined by its enum constants. Hence new can not be used to instantiate an enum.

An enum type has no instances other than those defined by its enum constants. It is a compile-time error to attempt to explicitly instantiate an enum type (§15.9.1).

Refer this

Upvotes: 11

lol
lol

Reputation: 3390

You can use something like this, you do not need static List but the method has to be static as answered by Kent,

public static ClassA getClassAByCDValue(String cdValue)
{
    for(ClassA value: ClassA.values())
    {
        if(value.cdValue.contains(cdValue))
        {
            return value;
        }
    }
    return null;
}
public static boolean isCDValue(String cdValue)
{
    for(ClassA value: ClassA.values())
    {
        if(value.cdValue.contains(cdValue))
        {
            return true;
        }
    }
    return false;
}

Using above will be more appropriate as you just have to take care with adding/removing items in enum.

Upvotes: 0

Kent
Kent

Reputation: 195039

If you have to put the checking method in the Enum, I think it should be static

you can do this check:

ClassA.isCdValue(para)

Note that, you cannot new an Enum object. So if the method in your Enum, and it is not static, you cannot call it unless you have an Instance. but the goal of your method is checking if the string could be an instance.

another possibility is, use an immutable collection in your Enumm, and make it static and public. Then you could just call ClassA.CD_VALUES.contains(para)

Upvotes: 5

Miquel
Miquel

Reputation: 15675

If you want to access it from ClsA, you will have to make it static, if you want to access it from an instance of ClsSa then it doesn't.

A couple of other things: where do you declare clsValues in the first place?

There's no need for the complex if, you may replace this:

public boolean isCdValue(String cdValue)
    {
        if clsValues.contains(cdValue)
            return true;
        else return false;
    }   

with this

public boolean isCdValue(String cdValue){
    return clsValues.contains(cdValue)
}   

Last little thing, I'd strongly suggest you put curly braces around all your if and else's clauses, I've spent many a debugging hour because someone added a second line under the else, fooled by the indent and thinking it would only execute on the else.

Upvotes: 1

Related Questions