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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:12:35+00:00 2026-05-15T08:12:35+00:00

I have read the question Difference of Enum between java and C++? but I’m

  • 0

I have read the question Difference of Enum between java and C++? but I’m still confused.

I would like the following to return the related String:

public enum Checker {
    EMPTY ("Empty"),
    RED ("Red"),
    YELLOW ("Yellow");
}

From what I have read, this should be possible. Just would like you to shed some light on it on how to implement it.

  • 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-15T08:12:36+00:00Added an answer on May 15, 2026 at 8:12 am

    Short Answer

    You need a constructor, a field and a getter.

    Constructors

    Enum types can have constructors, provided that their access level is either private or default (package-private). You can not directly call these constructors, except in the enum declaration itself. Similar to classes, when you define an enum constant without parameters, you actually call the default constructor generated by the compiler. E.g.

    public enum King {
        ELVIS
    }
    

    is equivalent to

    public enum King {
        ELVIS() // the compiler will happily accept this
    }
    

    And just like in classes, if you define an explicit constructor, the compiler will not insert a default constructor, so this will not compile:

    public enum King {
        ELVIS, // error, default constructor is not defined
        MICHAEL_JACKSON(true)
        ;
        private boolean kingOfPop;
        King(boolean kingOfPop){this.kingOfPop = kingOfPop;}
    }
    

    This is a pretty good reference on enums that also explains the constructor issues.

    Fields and Accessors

    Enums are constants and are immutable as such. They can however define fields, that can have state. This is an awful practice, because developers will expect enums and their associated values to be constants, but you can still define a non-final field in an enum with getters and setters.

    This is legal java code:

    public enum Color {
        RED("FF0000"),
        GREEN("00FF00"),
        BLUE("0000FF");
        private String code;
        public String getCode(){return code;}
        public void setCode(String code){this.code = code;}
        private Color(String code){this.code = code;}
    }
    

    But it enables evil code like this:

    String oldBlue = Color.BLUE.getCode();
    Color.BLUE.setCode(Color.RED.getCode());
    Color.RED.setCode(oldBlue);
    

    So in 99.99 % of cases: if you have fields in your enums, you should make them final and provide getters only. If the fields are not immutable themselves, provide defensive copies:

    public enum Band {
        THE_BEATLES("John","Paul","George","Ringo");
        private final List<String> members;
        public List<String> getMembers(){
            // defensive copy, because the original list is mutable
            return new ArrayList<String>(members);
        }
        private Band(String... members){
            this.members=Arrays.asList(members);
        }
    }
    

    Solution

    In your case it’s very simple: you just need a single field of type string (immutable), so initializing it in the constructor and providing a getter is perfectly ok:

    public enum Checker {
    
        EMPTY ("Empty"),
        RED ("Red"),
        YELLOW ("Yellow");
    
        private final String value;
    
        private Checker(final String value) {
            this.value = value;
        }
    
        public String getValue() { return value; }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have read the topic about pointer, but i still have some question. //
I've read this question: What's the difference between identifying and non-identifying relationships? But I'm
I have read this question and the simple and clear answer but it's not
I have read this question but it's not quite what I was looking for.
I have read other related question but i cant really get them to relate
What is the difference between an error and an exception? I have read numerous
Ok, I hope this question makes some sense but what is the difference between
I have read about different types of virtualization in Wikipedia but my question is:
I have read some different responses for this same question, so I dont know
I have read many question about improving the performance of C++ and C code

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.