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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:05:23+00:00 2026-05-16T17:05:23+00:00

Is there a best practice for enumerations in java? For example, I have the

  • 0

Is there a best practice for enumerations in java? For example, I have the following:

class Foo {

    public static final int OPTION_1 = 'a';
    public static final int OPTION_2 = 'b';

    public void doSomething(String name, int option) {
       ...
    }
}

void test() {
    Foo foo = new Foo();
    foo.doSomething("blah", Foo.OPTION_2);
}

so the user can choose to use one of the static ints defined in Foo, but they could also supply any other int they want, there’s no compile-time checking on it. Is there some way around this in java, some other way of doing this to restrict the end developer to choose from only the defined option types?

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-16T17:05:23+00:00Added an answer on May 16, 2026 at 5:05 pm
    class Foo {
        public enum Option{First, Second}
        public void doSomething(String name, Option option) {
           ...
        }
    }
    
    void test() {
        Foo foo = new Foo();
        foo.dosomething("blah", Foo.Option.Second);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a best practice for making private methods in classes static? I have
Is there a best-practice or common way in JavaScript to have class members as
Is there a best practice pattern to model the following parent child relationship where
Is there a best practice or noticeable difference in performance between using Java to
My question is best illustrated with an example. Suppose I have the enum: public
Is there any Best Practice approach to storing persistent static data for iPhone apps?
Is there a best practice for importing static library headers in Xcode 4? Most
Is there best practice about display errors in during controller action executing on view?
Is there best practice for managing Object shared by 2 or more others Object.
Is there a best practice for making an entity immutable? Users create exercises in

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.