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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T13:25:52+00:00 2026-05-19T13:25:52+00:00

I have a method which takes an enum and uses it in some fashion.

  • 0

I have a method which takes an enum and uses it in some fashion. The issue is that I have many different enum types and am not what acceptable practice is to pass an enum to a method.

  • 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-19T13:25:52+00:00Added an answer on May 19, 2026 at 1:25 pm

    I’m assuming that you mean you have a bunch of different enum classes that mean separate things, and that you want to pass them into one method.

    To do that, use a marker interface:

    public interface SpecialEnumType {
    }
    

    then:

    public enum MySpecialEnumType implements SpecialEnumType {
      ...
    }
    
    public enum AnotherSpecialEnumType implements SpecialEnumType {
      ...
    }
    

    Now your method will accept a parameter of type SpecialEnumType:

    public doSomething(SpecialEnumType specialEnumType) {
      ...
    }
    

    Having done that, you can do:

    obj.doSomething(MySpecialEnumType.SomeThing);
    obj.doSomething(AnotherSpecialEnumType.SomethingElse);
    

    In general, it’s perfectly alright to use an enum as a parameter type for a method argument.

    UPDATE

    I’ve used this pattern while integrating with third-party API’s. For example, a little while ago I had to integrate with different shipping providers. To do this, I provided a general interface that allowed the developer to send in shipping information (like the addresses, packages, weights, packing options, etc.). If you wanted to implement integration with a new provided, all you needed to do was implement the interface.

    Now each shipping provider had its own set of options. Before using marker interfaces, I had a single enum which contained all the options (of all the different shipping providers). This is obviously hard to maintain. But I couldn’t split the enums into different classes because the interface specified a specific type of enum for the method arguments.

    Using a marker interface, I was able to get around this problem. I created an interface called ShippingProviderOption. Then for each provider, I extended the interface and created an enum, with the specific options for that provider. This way I was able to separate out the options, but still present a common interface.

    As far as code is concerned (greatly simplified and somewhat contrived, for demonstration purposes):

    public interface ShippingProviderOption {
    }
    
    public enum UPSOption implements ShippingProviderOption {
        ...
    }
    
    public enum FedexOption implements ShippingProviderOption {
        ...
    }
    
    public interface ShippingProvider {
    
       public ShippingResponse ship(ShippingProviderOption option);
    }
    
    public class UPSProvider implements ShippingProvider {
    
       @Override
       public ShippingResponse ship(ShippingProviderOption option) {
    
           if(option == UPSOption.PackageType) {
              ...
           }
       }       
    }
    
    public class FedexProvider implements ShippingProvider {
    
       @Override
       public ShippingResponse ship(ShippingProviderOption option) {
    
           if(option == FedexOption.PickupType) {
              ...
           }
       }       
    }
    

    Now in my actual implementation, I have a few methods in the marker interface. So it doesn’t really even have to be a marker interface; it can contain methods.

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

Sidebar

Related Questions

I have a method which takes String argument. In some cases I want to
I have a method which takes an Enum. Say method is methodName(MyTypes) where MyTypes
Can I have a method which takes arguments that are denoted with the same
I have a method which takes params object[] such as: void Foo(params object[] items)
i have a method which takes in a DotNetOpenAuth (formally known as DotNetOpenId) Response
I have a method which takes a directory path as a string. In the
My problem is the following. I have a method which simply takes an XML
I have a method which should be executed in an exclusive fashion. Basically, it's
I have a Java method which takes a String as parameter and returns a
I have model that is a queue of Strings associated with enum types. I'm

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.