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

  • Home
  • SEARCH
  • 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 8011445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:01:04+00:00 2026-06-04T19:01:04+00:00

I have some enums, all different, and I want to create a function that

  • 0

I have some enums, all different, and I want to create a function that can find or not if a string is one of enum variable name (not sure it’s really understandable).

enum MYENUM {
  ONE,
  TWO;
}

enum MYENUM1 {
  RED,
  GREEN;
}

I want to do this (this is just for the example, my enum are more complicated):

if(isInEnum(MYENUM, "one")) ...
if(isInEnum(MYENUM1, "one")) ...

isinEnum function (the code is bad, it’s just for understanding):

boolean isinEnum(enum enumeration, String search) {
  for(enum en : enumeration.values()){
    if(en.name().equalsIgnoreCase(search)) return true;
  }
  return false;
}

Is this kind of thing possible?

I think not, according to what I can read on the web, but maybe someone has a solution to do this, instead of making one loop for each enum.

  • 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-06-04T19:01:06+00:00Added an answer on June 4, 2026 at 7:01 pm

    Here is a way using reflection…

    public class EnumFinder {
    
        public static <T extends Enum<T>>  boolean isInEnum(Class<T> clazz, String name) {
            for (T e : clazz.getEnumConstants()) {
                if (e.name().equalsIgnoreCase(name)) {
                    return true;
                }
            }
    
            return false;
        }
    
        public static void main(String[] argv) {
            System.out.println(isInEnum(MYENUM.class, "one"));   // true
            System.out.println(isInEnum(MYENUM1.class, "one"));  // false
        }
    }
    

    Your attempt in your answer was actually very close. The only difference is that Java needs an instance of the defining class in order to answers questions about an unknown type dynamically at runtime.

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

Sidebar

Related Questions

i have an java-constraint-interface declaring some enums. public interface MyClass{static enum BOOP{a,b,c,d,e,f}}; how can
I have some custom types. They are all basically enums. Here is an example
I have a requirement to create some objects that implement a given interface, where
I have some code that I want to refactor. I have lots of methods
I have three different Enum and all of three has same identifier but different
Would the following make the objects fulfil all characteristics that enums have in JavaScript?
I have some issues resolving an algorithm used to find all posible combinations taking
Suppose we have some named enums: enum MyEnum { FOO, BAR = 0x50 };
I have created some enums as .dll dynamically using data from database. I am
I have to add some enum options to a database table. The problem being,

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.