Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6761677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:13:15+00:00 2026-05-26T14:13:15+00:00

I have set of constants like: public static final String CONST1=abc; public static final

  • 0

I have set of constants like:

public static final String CONST1="abc";
public static final String CONST2="cde";
public static final String CONST3="ftr";
...................................
public static final String CONSTN="zya";

In an application, I need to check if some value is in some set of constants, like:

if (String val in [CONST1,CONST2,CONST3]) {
do something;}
else {
....

Is it possible to do this with enum? Or is it better use a set or an array?
Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T14:13:15+00:00Added an answer on May 26, 2026 at 2:13 pm

    What you need is Enum.valueOf()

    Returns the enum constant of the specified enum type with the
    specified name. The name must match exactly an identifier used to
    declare an enum constant in this type. (Extraneous whitespace
    characters are not permitted.)

    Here is an example of what I usually do when working with Enums and might have “bad” data coming in.

    public class EnumTest
    {
        public static void main(final String[] args)
        {
            final Option o = Option.safeValueOf(args[0]);
            switch(o)
            {
                case CHOICE_A: // fall through
                case CHOICE_B: // fall through
                case CHOICE_C: // fall through
                    System.out.format("You selected %s", o );
                    break;
                case CHOICE_D:
                    System.out.format("You selected %s", o);
                    break;
                default:
                    System.out.format("Default Choice is %s", o );
            }
        }
    
        public enum Option
        {
            UNRECOGNIZED_CHOICE, CHOICE_A, CHOICE_B, CHOICE_C;
    
            // this hides the Exception handling code
            // so you don't litter your code with try/catch blocks
            Option safeValueOf(final String s)
            {
                try
                {
                    return Option.valueOf(s);
                }
                catch (final IllegalArgumentException e)
                {
                    return UNRECOGNIZED_CHOICE;
                }
            }
        }
    }
    

    you can also convert the Array of values from Option.values() into an EnumSet and search them and avoid the overhead of the possible Exception if you think it will be getting lots of bad data.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to set some public static constants on a class conditionally by
in my config.php where i have all constants i set the PATH to a
Suppose I have a MEF composition like this: public class Composition { [ImportMany(AllowRecomposition =
I found out that object constants in PHP always have public visibility so it
I have a class say public class CostStore { int DirectorateId { get; set;
I have custom attribute defined like so: [AttributeUsage(AttributeTargets.Field)] public class EnumDisplayAttribute : Attribute {
I have a class declaring constants for my app public class GroupConstants { ..
I have the following class: public class Test { public string Text { get;
I try to build extension method for IQuerable like this: public static IQueryable<T> FilterByString<T>(this
I have code like this... class Time { public: Time(int, int, int); void set_hours(int);

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.