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 8801975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:58:14+00:00 2026-06-14T00:58:14+00:00

I want to do something like this: public class MyEnumTest { public enum MyEnum;

  • 0

I want to do something like this:

public class MyEnumTest {

    public enum MyEnum;
    private MyEnum value;

    public MyEnumTest(enum e)
    {
        this.MyEnum = e; 
        this.value = this.MyEnum.values[0]//Set value to the first enum value.
    }

    public void setValue(MyEnum e)
    {
        this.value = e; 
    }

    public MyEnum getValue()
    {
        return value;
    }

}

and call it something like this:

MyEnumTest car = new MyEnumTest({HONDA, FORD, TOYOTA, HOLDEN}); 
MyEnumTest fruit = new MyEnumTest({APPLE, BANANA, CHERRY}); 

/* some operations occur*/ 

if (car.getValue() == car.FORD) System.out.println("Vrooom!"); 
if (fruit.getValue()) == fruit.APPLE) System.out.println ("Is red and round and delicious"); 

Any ideas for doing this?

For bonus marks – would it be possible to set the initial value using the enum values?
eg something like

MyEnumTest myET = new myEnumTest(new enum {RED, GREEN, BLUE, YELLOW}, BLUE); 
  • 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-14T00:58:15+00:00Added an answer on June 14, 2026 at 12:58 am

    As others said it’s hard to understand what you are trying to do, but here is my best guess:

    public enum Color {
        RED, GREEN, YELLOW, BLUE;
    }
    
    
    public class MyEnumTest<E extends Enum<E>> {
        private final Class<E> enumType;
        private final E value; 
    
        public Class<E> getEnumType() {
            return enumType;
        }
    
        public E getValue() {
            return value;
        }
    
        public MyEnumTest(Class<E> enumType, E value) {
            this.enumType = enumType;
            this.value = value;
        }
    
        public MyEnumTest(Class<E> enumType) {
            this(enumType, enumType.getEnumConstants()[0]);
        }
    
        public static void main(String... args) {
            // no initial value provided
            MyEnumTest<Color> myET = new MyEnumTest<Color>(Color.class);
            if (myET.getValue() == Color.RED) {
                System.out.println("It's probably an apple");
            }
    
            // providing an initial value
            myET = new MyEnumTest<Color>(Color.class, Color.YELLOW);
            if (myET.getValue() == Color.YELLOW) {
                System.out.println("It's probably a banana");
            }
        }
    }
    

    Also, keep in mind that enum values are static… So, something like myET.MyEnum.RED doesn’t make sense… You should use instead: Color.RED

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

Sidebar

Related Questions

I want to do something like this: @Stateless public class GreeterEjb { private final
I want something like this public abstract class abc { public abstract void test();
I want to do something like this: public class ScadenzaService { ... public List<Scadenza>
Like I would want to do something like this, class Object { public: World
I want to achieve something like this in C# 3.5: public void Register<T>() :
I want to do something like this public class Class1 { public Class1() {
I have something like this: public class Foo { public Bar[] Bars{get; set;} }
Suppose I have something like this: public abstract class AbstractDataObject { public abstract void
I want to do something like this: @Entity public class Bar { @Id @GeneratedValue
i want something like this : public class Order { public Guid OrderID {

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.