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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:37:25+00:00 2026-05-25T01:37:25+00:00

Java allows enum as values for annotation values. How can I define a kind

  • 0

Java allows enum as values for annotation values. How can I define a kind of generic default enum value for an enum annotation value?

I have considered the following, but it won’t compile:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public <T extends Enum<T>> @interface MyAnnotation<T> {

    T defaultValue();

}

Is there a solution to this issue or not?

BOUNTY

Is does not seem like there is a direct solution to this Java corner case. So, I am starting a bounty to find the most elegant solution to this issue.

The ideal solution should ideally meet the following criteria:

  1. One annotation reusable on all enums
  2. Minimum effort/complexity to retrieve the default enum value as an enum from annotation instances

BEST SOLUTION SO FAR

By Dunes:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface MyAnnotation {

    // By not specifying default,
    // we force the user to specify values
    Class<? extends Enum<?>> enumClazz();
    String defaultValue();

}

...

public enum MyEnumType {
    A, B, D, Q;
}

...

// Usage
@MyAnnotation(enumClazz=MyEnumType.class, defaultValue="A"); 
private MyEnumType myEnumField;

Of course, we can’t force the user to specify a valid default value at compile time. However, any annotation pre-processing can verify this with valueOf().

IMPROVEMENT

Arian provides an elegant solution to get rid of clazz in annotated fields:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface MyAnnotation {

}

...

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@MyAnnotation()
public @interface MyEnumAnnotation {

    MyEnumType value(); // no default has user define default value

}

...

@MyEnumAnnotation(MyEnum.FOO)
private MyEnumType myValue;

The annotation processor should search for both MyEnumAnnotation on fields for the provided default value.

This requires the creation of one annotation type per enum type, but guarantees compile time checked type safety.

  • 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-25T01:37:26+00:00Added an answer on May 25, 2026 at 1:37 am

    I’m not sure what your use case is, so I have two answers:

    Answer 1:

    If you just want to write as little code as possible, here is my suggestion extending Dunes’ answer:

    public enum ImplicitType {
        DO_NOT_USE;
    }
    
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.FIELD)
    public @interface MyAnnotation {
    
        Class<? extends Enum<?>> clazz() default ImplicitType.class;
    
        String value();
    }
    
    @MyAnnotation("A"); 
    private MyEnumType myEnumField;
    

    When clazz is ImplicitType.class, use the fields type as enum class.

    Answer 2:

    If you want to do some framework magic and want to maintain compiler checked type safety, you can do something like this:

    /** Marks annotation types that provide MyRelevantData */
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.ANNOTATION_TYPE)
    public @interface MyAnnotation {
    }
    

    And in the client code, you would have

    /** Provides MyRelevantData for TheFramework */
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.FIELD)
    @MyAnnotation
    public @interface MyEnumAnnotation {
    
        MyEnumType value(); // default MyEnumType.FOO;
    
    }
    
    @MyEnumAnnotation(MyEnum.FOO)
    private MyEnumType myValue;
    

    In this case you would scan the field for annotations which again are annotated with MyAnnotation. You will have to access the value via reflection on the annotation object, though. Seems like this approach is more complex on the framework side.

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

Sidebar

Related Questions

How come java doesn't allow the following generic return type: public <T extends Enum<T>
I have a web application (Java, Websphere, JSP) which allows co-workers to register visitors
I'm writing a web app (Java) which allows users to select contacts. The contacts
I've written a fairly simple java application that allows you to drag your mouse
In several modern programming languages (including C++, Java, and C#), the language allows integer
I am using GAE(Java) with JDO for persistence. I have an entity with a
Does Java have the possibility of anonymous enums? For example, I want my class
I want to create a form (flash or java/ajax) that allows selection data to
In Java, the throws keyword allows for a method to declare that it will
On one box there is a running java server I create. Another app allows

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.