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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:36:14+00:00 2026-05-26T15:36:14+00:00

Often a class has to be configured using a number of boolean options. What

  • 0

Often a class has to be configured using a number of boolean options. What are recommended ways in Java to specify execution configurations?

I can think of 4 ways how options can be passed to the class.

  1. Using bitmasks. Efficient, easy to use, but not grouped together. More difficult to find via autocomplete amongst other members. Restricted in size.

    public static final int A_ENABLED = 1 << 1;
    public static final int B_ENABLED = 1 << 2;
    
  2. Using (inner) parameter class. Difficult to create, too verbose.

    public static class Params {
        private final boolean enabledA;
        private final boolean enabledB;
    }
    
    public class MyFunctionality {
        private final SomeOtherArguments myDataArguments;
        ...
        private final Params params;
    
        MyFunctionality(SomeOtherArguments myDataArguments, Params params);
    }
    
  3. Using boolean fields as part of the class. Increases verbosity of the class (extra arguments in the constructor you have to worry about). Constructor of the class has to be modified everytime parameters change. In 2 option only Params class will change.

    public class MyFunctionality {
        private final SomeOtherArguments myDataArguments;
        ...
        private final boolean enabledA;
        private final boolean enabledB;
        private final boolean enabledC;
        private final boolean enabledD;
        ...
    
        public MyFunctionality(SomeOtherArguments someOtherArguments, boolean enabledA, boolean enabledB, boolean enabledC, boolean enabledD);
    }
    
  4. Using enums.

    public static enum Params {
        OPTION_A, OPTION_B
    }
    

Can you think of other better ways to do that?

  • 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-26T15:36:14+00:00Added an answer on May 26, 2026 at 3:36 pm

    Enums and the EnumSet are specifically designed for this.

    class myClass {
    
      public static enum Options {A,B,C};
    
      private EnumSet optionSet = EnumSet.noneOf(Option.class);
    
      public setOption(Option o) {
        optionSet.add(o);
      }
      public setOptions(EnumSet addset) {
        optionSet.addAll(addset);
      }
    
    }
    

    optionSet contains the options that are ‘set’. EnumSet is extremely efficient, using just one bit for each enum element in most implementations, and allowing get and set in constant time. You can of course restrict setting options to construction time as well.

    EDIT: You can set multiple options such as A and B simultaneously with:

    myClassInstance.setOptions(EnumSet.of(A,B));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I often auto-generate an class's hashCode() method using IntelliJ IDEA and typically the method
When running the following class the ExecutionService will often deadlock. import java.util.ArrayList; import java.util.Collection;
I often come accross the problem that I have a class that has a
The java code I'm working on at the moment has often a structure like
In my Java code I often use the very handy method(Class... args) varargs. As
One can often see class names carrying a reference to the namespace to which
Often, one needs several enumerated types together. Sometimes, one has a name clash. Two
I often find I am writing a class similar to the following (but with
I often refactor code first by creating an inner class inside the class I'm
I often find the need to do something along these lines: public class OperationResult

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.