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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:33:20+00:00 2026-06-10T15:33:20+00:00

For my project, I have many objects split into 10 classes. Each of the

  • 0

For my project, I have many objects split into 10 classes. Each of the objects may perform some operations, which have to be registered beforehand (the registering of operations is done only once per class). The operations defined for each class are represented as public final static integers. I would like to dynamically assign the ID of operations at runtime (the number of operations per class is currently about 20 and the number will increase).

The problem arises, when an operation is performed and I have to find which operation is being performed (I use a switch statement).

Here is a simple example of working code:

public class Test {
    final static int foo = 8;
    final static int bar = 10;

    public static void main(String[] args)
    {
        int x=10;

        switch(x)
        {
        case foo:
            System.out.println("FOO");
            break;
        case bar:
            System.out.println("BAR");
            break;
        default:
            System.out.println("PROBLEM");
        }
    }
}

This code normally compiles and displays BAR.

But this slightly transformed code produces an Unresolved compilation problem with case expressions must be constant expressions.

public class Test {
    final static int foo;
    final static int bar;

    static
    {
        foo=8;
        bar=10;
    }
    public static void main(String[] args)
    {
        int x=10;

        switch(x)
        {
        case foo:
            System.out.println("FOO");
            break;
        case bar:
            System.out.println("BAR");
            break;
        default:
            System.out.println("PROBLEM");
        }
    }
}

Shouldn’t these codes actually work and compile the same?
I am not able to do anything dynamically until I resolve this problem? Or is any other way?

Thanks

EDIT:
Due to ideas for using enums I would like to solve this problem:

public class Test {
  enum OperationSet1 {
    FOO, BAR, THESE, ARE, ALL, DIFFERENT, OPERATIONS
  }
  enum OperationSet2 {
    FOO, BAR, NOW, SOME, OTHER, OPS
  }

  public static void main(String[] args) {
    OperationSet1[] ops = new OperationSet1[10];
    for (int i=0; i<ops.length; i++)
      ops[i] = OperationSet1.values()[(int)(Math.random()*OperationSet1.values().length)];

    OperationSet2[] ops2 = new OperationSet2[10];
    for (int i=0; i<ops.length; i++)
      ops[i] = OperationSet2.values()[(int)(Math.random()*OperationSet2.values().length)];

    for (OperationSet1 op:ops)
      handleOperation(op);
  }
    for (OperationSet2 op:ops2)
      handleOperation(op);
  }
  public static void handleOperation(Object? op) {
    switch(op) {
        /**code to handle**/
    }
  }
}

We have two enums and I would like to have one handling function (prefferably with switch statement), which would handle all the cases apperaing in these two enums.

EDIT2:
So here goes. I have 10 classes (C1, C2, ..., C10) and around 40 objects of these classes. Some of these objects are so-called owners and some are shared. Each shared object has an owner (this is the basic relation – it does not have anything to do with java inheritance).

Now each of the shared objects obj changes from time to time, and this obj needs to ask its owner own if it may change (this is only one of the operations) and then it changes (and again notifies the owner). Object obj also has a predefined set of operations which he may perform, defined in C1 by an enum. Hence own has to have a handling function for object operations of class C1. Now we have an object obj2 which is of class C2, has a different set of operations and the same owner own. obj may perform only operations defined in C1 and obj2 may perform only operations defined in C2.

How to generalize the handling function so it will be neatly written? Should I even use enums?

  • 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-10T15:33:22+00:00Added an answer on June 10, 2026 at 3:33 pm

    You can only use constants known at compile time in a switch statement. This is because switch code is checked and built statically.

    In the second case you could do

    static
    {
        foo=8;
        bar=foo;
    }
    

    but as these values are not known until runtime, there would be no way to either built the switch statement or check it is correct.

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

Sidebar

Related Questions

I'm working on a large project (for me) which will have many classes and
I have a project in which I have many objects with many properties whose
i am developing a project in which i have used many classes. for creating
ok i have a project which has many gridview in its pages... now i
I have a project with several CSS files, each with many different settings. Every
In our project we have several production databases and many devs. Each production database
If I have two objects Project and Task associated with one-to-many relation Project.hasMany(Task) var
my team have built many tools for our project using win forms and Visual
I have worked many projects with no problems. But in current project, I tested
In my project, I have so many .png images. They are working properly in

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.