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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:31:25+00:00 2026-05-22T14:31:25+00:00

I have a class that have a number of constants: public class SecurityConstants {

  • 0

I have a class that have a number of constants:

public class SecurityConstants {
    private static final String HAS_ROLE_TEMPLATE = "hasRole('%s')";

    public static final String ROLE_USER_INTERNAL = "ROLE_USER_INTERNAL";
    public static final String HAS_ROLE_USER_INTERNAL = String.format(HAS_ROLE_TEMPLATE, ROLE_USER_INTERNAL);
}

If I then try to use HAS_ROLE_USER_INTERNAL as @PreAuthorize annotation attribute value like this @PreAuthorize(SecurityConstants.HAS_ROLE_USER_INTERNAL) compiler fails with:

The value for annotation attribute
PreAuthorize.value must be a constant
expression

However if I change HAS_ROLE_USER_INTERNAL to be a simple String it works just fine:

public static final String HAS_ROLE_USER_INTERNAL = "hasRole('ROLE_USER_INTERNAL')";

What’s the problem with using String.format()? Field is static and final, what can possibly go wrong?

  • 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-22T14:31:26+00:00Added an answer on May 22, 2026 at 2:31 pm

    The value of String.format() isn’t known at compile time, whereas a String literal is.

    Since the annotations are metadata on the compiled class, their values must be known by the time the compiler generates the final .class file. Since String.format()‘s value will only be known once the code is actually run, the compiler won’t let you use it as part of an annotation.

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

Sidebar

Related Questions

I have a class Constants in which I store a number of static readonly
If I have a class that contains a number of typedef 'd variables, like
I have a class that spawns an arbitrary number of worker object that compute
I have a Version class that contains a version_number string, and can obtain a
This is a C++ class that I have made with n number of pointers.
I have a number of classes that register with a notification class in my
I have a function that accepts a variable number of parameters: foo (Class... types);
I'm testing a C++ class with a number of functions that all have basically
I have class that looks like this: class A { public: class variables_map vm
I have this extension method for my BusinessObject class: public static class Extensions {

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.