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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:49:04+00:00 2026-05-18T09:49:04+00:00

Correspondig the following question: Java: Enum parameter in method I would like to know,

  • 0

Correspondig the following question:

Java: Enum parameter in method

I would like to know, how can I format the code to require enums generically.

Foo.java

public enum Foo { 
    a(1), b(2);
}

Bar.java

public class Bar {
    public Bar(generic enum);
}

Later on I’ll have more enum classes like “foo”, but you can still create bar containing any kind of enum class. I have “jdk1.6.0_20” by the way…

  • 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-18T09:49:04+00:00Added an answer on May 18, 2026 at 9:49 am

    See the methods in EnumSet for reference, e.g.

    public static <E extends Enum<E>> EnumSet<E> of(E e)
    

    (This method returns an EnumSet with one element from a given Enum element e)

    So the generic bounds you need are: <E extends Enum<E>>


    Actually, you will probably make Bar itself generic:

    public class Bar<E extends Enum<E>> {
    
        private final E item;
    
        public E getItem(){
            return item;
        }
    
        public Bar(final E item){
            this.item = item;
        }
    }
    

    You may also add a factory method like from, with etc.

    public static <E2 extends Enum<E2>> Bar<E2> with(E2 item){
        return new Bar<E2>(item);
    }
    

    That way, in client code you only have to write the generic signature once:

    // e.g. this simple version
    Bar<MyEnum> bar = Bar.with(MyEnum.SOME_INSTANCE);
    // instead of the more verbose version:
    Bar<MyEnum> bar = new Bar<MyEnum>(MyEnum.SOME_INSTANCE);
    

    Reference:

    • Java Tutorial – Learning the Java Language
      • Classes and Objects > Enum Types
      • Generics
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question is self explanatory if you know how to use JMockit: How do
I am writing code in Java that uses an unordered, rooted tree where each
How do I convert a string into the corresponding code in PLT Scheme (which
In C# and in Java (and possibly other languages as well), variables declared in
This may be a stupid question, but I have to ask! I have the
I want a unidirectional one-to-one relationship between objects of 3 java classes: Person to
I'd like to maintain a history of jobs that were scheduled by a Quartz
This is a question corresponds to my uni assignment so I am very sorry
Consider the following mark-up input: * Line 1 * Line 2 :* Line 2.1
I have a memory buffer corresponding to my screen resolution (1280x800 at 24-bits-per-pixel) that

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.