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

  • Home
  • SEARCH
  • 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 3407672
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:46:47+00:00 2026-05-18T05:46:47+00:00

I have a class declaring constants for my app public class GroupConstants { ..

  • 0

I have a class declaring constants for my app

public class GroupConstants {
    ..
    public static final int INTEGER_VALUE = 1;
    public static final int LONG_VALUE = 2;
    public static final int STRING_VALUE = 3;
    ..
}

In the code there is a set of switch statements

private static Object getValue(String stringValue, Parameter parameter) throws InvalidPatternException
{
    Object result=null;
    switch (parameter.getDataType())
    {
        case GroupConstants.STRING_VALUE: // String value
            result=stringValue;
        break;
        case GroupConstants.INTEGER_VALUE: // Long value
        case GroupConstants.LONG_VALUE:
        case GroupConstants.BOOLEAN_VALUE:
        case GroupConstants.DATE_VALUE:
..
}

I want to refactor the int constant values to be represented by an enum

public enum DataType {

    UNKNOWN_VALUE(0,"unknown"),
    INTEGER_VALUE(1,"integer"),
    LONG_VALUE(2,"long"),
    STRING_VALUE(3,"string"),
    BOOLEAN_VALUE(4,"boolean"),
..
}

so my code might look like this

@Deprecated
public static final int INTEGER_VALUE = DataType.INTEGER_VALUE.getId();

and overtime i can change my switch statements. When i change the static final int reference to point to the enum all my switch statements break.

[javac] /home/assure/projects/tp/main/src/a/b/c/DDDDDManagerBean.java:1108: constant expression required
[javac]             case GroupConstants.INTEGER_VALUE:
[javac]                                ^
[javac] /home/assure/projects/tp/main/src/a/b/c/ParameterComponent.java:203: constant expression required
[javac]         case GroupConstants.INTEGER_VALUE:
[javac]                            ^
[javac] /home/assure/projects/tp/main/src/a/b/c/ParameterComponent.java:268: constant expression required
[javac]             case GroupConstants.INTEGER_VALUE:
[javac]                                ^
[javac] /home/assure/projects/tp/main/src/a/b/c/ParameterComponent.java:316: constant expression required
[javac]             case GroupConstants.INTEGER_VALUE:
[javac]                                ^
[javac] /home/assure/projects/tp/main/src/a/b/c/ParameterComponent.java:436: constant expression required
[javac]         case GroupConstants.INTEGER_VALUE:

I don’t want to be forced to change all the switches yet, so its there a clean work around?

  • 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-18T05:46:48+00:00Added an answer on May 18, 2026 at 5:46 am

    That won’t work. The problem is that the getId() call means that the constant’s value expression is not a compile-time constant expression according to the JLS. Refer to JLS 15.28 Constant Expressions for the gory details, and you will see that method calls are not allowed in constant expressions.

    I don’t think there is any workaround, apart for a large-scale change to all of the switch statements. But I wouldn’t worry. Your IDE should be able help you find and replaces all occurrences of the old constants.

    FOLLOWUP

    The following code from your comment won’t work either:

    private int INTEGER_VALUE_HOLDER = DataType.INTEGER_VALUE.getId(); 
    public static final int INTEGER_VALUE = INTEGER_VALUE_HOLDER;
    

    Firstly, INTEGER_VALUE_HOLDER is not a “constant variable” according to the definition in JLS 4.12.4 Final Variables. Therefore neither is INTEGER_VALUE.

    Secondly, the initializer expression for a static cannot refer to this, and INTEGER_VALUE_HOLDER is really another way of saying this.INTEGER_VALUE_HOLDER.

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

Sidebar

Related Questions

I've seen examples like this: public class MaxSeconds { public static final int MAX_SECONDS
I have class A: public class ClassA<T> Class B derives from A: public class
If I have class ObjA { public ObjB B; } class ObjB { public
Say I have class A with class A { final String foo() { //
I have a class called fraction, and I'm declaring some operators as friends. I
I have a class, called Sprite: public abstract class Sprite { protected Texture2D Image
I have a class with the following structure: class Something { private static $_instance
I have this class used in the API demos: public class TextToSpeechActivity extends Activity
I have class method that returns a list of employees that I can iterate
I have: class MyClass extends MyClass2 implements Serializable { //... } In MyClass2 is

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.