The switch statement and initialization of a final static variable in a static block

For my project, I have many objects, divided into 10 classes. Each of the objects can perform some operations that must be registered in advance (registration of operations is performed only once for each class). The operations defined for each class are represented as public final staticintegers. I would like to dynamically assign an identifier for operations at runtime (the number of operations per class is currently around 20, and the number will increase).

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

Here is a simple working code example:

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 is usually compiled and displayed BAR.

Unresolved compilation problem 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");
        }
    }
}

? , ? ?

EDIT: - :

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**/
    }
  }
}

, ( switch), , .

EDIT2: . 10 (C1, C2, ..., C10) 40 . , . ( - Java).

obj , obj own, ( ), ( ). obj , , C1 enum. , own C1. obj2, C2, own. obj , C1, obj2 , C2.

, ? ?

+5
4

, switch. , .

static
{
    foo=8;
    bar=foo;
}

, switch , .

+4

static variables or blocks or methods (Runtime).

, / . , , (, ).

foo bar .

+2

. , .

public class Test {
  enum Operation {
    FOO, BAR, THESE, ARE, ALL, DIFFERENT, OPERATIONS
  }

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

    for (Operation op:ops)
      handleOperation(op);
  }

  public static void handleOperation(Operation op) {
    switch(op) {
    case FOO:
      System.out.println("FOO");
      break;
    case BAR:
      System.out.println("BAR");
      break;
    case THESE:
      System.out.println("THESE");
      break;
    case ARE:
      System.out.println("ARE");
      break;
    case ALL:
      System.out.println("ALL");
      break;
    case DIFFERENT:
      System.out.println("DIFFERENT");
      break;
    case OPERATIONS:
      System.out.println("OPERATIONS");
      break;
    default:
      System.out.println("PROBLEM");
    }
  }
}
0

OR just use if-elseif case:

private final static int ONE = 1;
private final static int TWO = 2;

public static void main(String[] args) {
int value = 1;

  if(value==ONE){

  }
  else if(value==TWO){

  }

}

0
source

All Articles