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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T19:38:28+00:00 2026-06-08T19:38:28+00:00

I see this scattered throughout code base: @RequestMapping(value = myValue) I would prefer to

  • 0

I see this scattered throughout code base:

@RequestMapping(value = "myValue")

I would prefer to use something like this:

@RequestMapping(value = Constants.myValue)

It seems to break DRY using the actual String value within @RequestMapping instead of constant.
But is this good code practice? Should I use an enum instead?
I may need to use Constants.myValue elsewhere in the code base.

  • 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-06-08T19:38:30+00:00Added an answer on June 8, 2026 at 7:38 pm

    Should I use an enum instead?

    You can’t. Annotation variables must be compile-time constants. Enums and String literals both are, but you can’t create an enum that is a String and @RequestMapping needs a String (and if your enum has a method that returns a String or a String field, that’s not a compile-time constant). Since there are multiple rounds of annotation processing it does work when the constant is in another class.

    That said: yes, I’d say using a dedicated constants class (perhaps several, for different types of constants) is a good practice that I use whenever I can (and it works with annotations as long as the constant a) is not defined inside the same compilation unit that has the annotation and b) is initialized in the declaration (as opposed to a static initializer block)).

    Here’s an example:

    Controller

    @Controller @RequestMapping(value = Mappings.CUSTOMER_PAGE)
    public class CustomerPageController{
        // methods here
    }
    

    Constants class

    public static final class Mappings{
        private Mappings(){}
        public static final String CUSTOMER_PAGE = "path/to/customer/page"
        // more constants
    }
    

    And here are some versions that won’t work:

    a)

    @Controller @RequestMapping(value = CUSTOMER_PAGE)
    public class CustomerPageController{
        private static final String CUSTOMER_PAGE = "path/to/customer/page";
    }
    

    This won’t compile because the annotation references a constant inside the class it annotates. This can’t work because during compilation, annotations are processed in a separate round before the rest of the code, while the class needs the annotation to already be processed for compilation (i.e. there’s a circular dependency between annotation and constant)

    b)

    public static final class Mappings{
        private Mappings(){}
        public static final String CUSTOMER_PAGE;
        static{
            CUSTOMER_PAGE = "path/to/customer/page"
        }
    
        // more constants
    }
    

    Although this is a static final field, it is not a compile-time constant, hence it can’t be used as an annotation parameter

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

Sidebar

Related Questions

Often I see code like this scattered and duplicated around source code: var handler
See this code example: http://jsfiddle.net/Z2BMK/ Chrome/IE8 look like this Firefox looks like this My
See this question. How to show popup message like in stackoverflow I am trying
See this code: TicketStoreService fakeTicketStoreService = MockRepository.GenerateMock<TicketStoreService>(); fakeTicketStoreService.Expect(service => service.DoSomething(Arg.Is(new Guid())) .Return(new Guid()); fakeTicketStoreService.DoSomething(Arg.Is(new
See this code: var jsonString = '{id:714341252076979033,type:FUZZY}'; var jsonParsed = JSON.parse(jsonString); console.log(jsonString, jsonParsed); When
see this link...: Exemple on this site i have this code <body> <div id=geral>
See this .What should i use instead of Jpegfile in the sample to represent
See this fiddle: http://jsfiddle.net/Bg9Zx/5/ Relevant code: <fieldset data-role=controlgroup data-type=horizontal id=locate-me> <input type=checkbox name=loc id=loc
See this page, Why doesnt it show like it does when you enter a
I'm working on some Django-code that has a model like this: class Status(models.Model): code

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.