1

I do have a enum class in java as follows

public enum SMethod {

/**
 * LEAVE IN THIS ORDER
 */
A    (true, true, true,false),
B    (true, true, false,false),
C    (true, true, false,false),
D    (false, false, false)

} 

Another class have below method

private String getSMethod(boolean isSds) {
    if (isClsSds)
        return "A";
    else 
        return "B";
}

Currently this method return hard code value but string.But I want to return it using SMethod enum.I have written it as follows:

private SMethod getSMethod(boolean isSds) {
    if (isClsSds)
        return SMethod.A;
    else 
        return SMethod.B;
}

but my need is this method should return String.

1
  • SMethod.A.name() should give you the string. Commented Apr 22, 2013 at 7:43

3 Answers 3

2

Use name() method:

return SMethod.A.name();

To get the String name of the enum object.

Sign up to request clarification or add additional context in comments.

Comments

2
return SMethod.A.name(); will return string

see name() method

Returns the name of this enum constant, exactly as declared in its enum declaration.

Comments

1

There are two ways.

So you can use

public String getName(SMethod enm)
{
    return enm.name();
    // or enm.toString();
}

Comments

Your Answer

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