2

I have created one static string in apex like

private static string HARSWARE_SOFTWARE= 'Hardware and Software';

and now this variable I am using in ENum like

public Enum PSServices{HARSWARE_SOFTWARE}

But when i print the service then I am getting variable HARSWARE_SOFTWARE not value?

In Java we can write

public Enum PSServices{HARSWARE_SOFTWARE('Hardware and Software')}

But same when i tried in salesforce apex i am getting error, I want string as return type.

Please assist me in this.

1 Answer 1

8

That's just not how enums work in Apex:

Although each value corresponds to a distinct integer value, the enum hides this implementation so that you don’t inadvertently misuse the values, such as using them to perform arithmetic. After you create an enum, variables, method arguments, and return types can be declared of that type.

and note that

Unlike Java, the enum type itself has no constructor syntax.

You should treat enums in Apex as abstract, valueless identifiers only. The value PSServices.HARSWARE_SOFTWARE is distinct from the variable HARSWARE_SOFTWARE existing in the outer scope.

2
  • So u mean I can't access the variable value which I have passed. I need to create a list of string rather than use ENUM with static variable?
    – user69533
    Commented Jun 7, 2019 at 20:07
  • 1
    @user69533 You cannot create enums with specific values at all. If you want to store string values, you'll need to use String-typed variables.
    – David Reed
    Commented Jun 7, 2019 at 20:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .