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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T06:49:28+00:00 2026-05-19T06:49:28+00:00

Today I was browsing through some questions on this site and I found a

  • 0

Today I was browsing through some questions on this site and I found a mention of an enum being used in singleton pattern about purported thread-safety benefits to such solution.

I have never used enums and I have been programming in Java for more than a couple of years now. And apparently, they changed a lot. Now they even do full-blown support of OOP within themselves.

Now why and what should I use enum in day to day programming?

  • 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-19T06:49:30+00:00Added an answer on May 19, 2026 at 6:49 am

    You should always use enums when a variable (especially a method parameter) can only take one out of a small set of possible values. Examples would be things like type constants (contract status: “permanent”, “temp”, “apprentice”), or flags (“execute now”, “defer execution”).

    If you use enums instead of integers (or String codes), you increase compile-time checking and avoid errors from passing in invalid constants, and you document which values are legal to use.

    BTW, overuse of enums might mean that your methods do too much (it’s often better to have several separate methods, rather than one method that takes several flags which modify what it does), but if you have to use flags or type codes, enums are the way to go.

    As an example, which is better?

    /** Counts number of foobangs.
     * @param type Type of foobangs to count. Can be 1=green foobangs,
     * 2=wrinkled foobangs, 3=sweet foobangs, 0=all types.
     * @return number of foobangs of type
     */
    public int countFoobangs(int type)
    

    versus

    /** Types of foobangs. */
    public enum FB_TYPE {
     GREEN, WRINKLED, SWEET, 
     /** special type for all types combined */
     ALL;
    }
    
    /** Counts number of foobangs.
     * @param type Type of foobangs to count
     * @return number of foobangs of type
     */
    public int countFoobangs(FB_TYPE type)
    

    A method call like:

    int sweetFoobangCount = countFoobangs(3);
    

    then becomes:

    int sweetFoobangCount = countFoobangs(FB_TYPE.SWEET);
    

    In the second example, it’s immediately clear which types are allowed, docs and implementation cannot go out of sync, and the compiler can enforce this.
    Also, an invalid call like

    int sweetFoobangCount = countFoobangs(99);
    

    is no longer possible.

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

Sidebar

Related Questions

Today I encountered this article about decimal expansion and I was instantaneously inspired to
Today when I was doing some experiments with == , I accidentally found out
I was browsing a coworkers c# code today and found the following: using (MemoryStream
Today I came across this question: you have a code static int counter =
Today I was tracking down a floating point exception in some code I had
I am using this IP Validation Function that I came across while browsing, it
Today we have received some strange exceptions on our production website. They all have
Today, while I was randomly reading the JavaScript patterns O'Reilly book, I found one
Just had my mind going today. I spent some time in IE debug mode,
Welcome, Today I found and decided to put pjax , but I could not

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.