-1

Lets take this for Example:

public class Vehicle {


   public enum Car {
      CAR1,
      CAR2,
      CAR3,
      CAR4,
   }


   public enum BIKE {
     BIKE1,
     BIKE2,
     BIKE3
  }


}


public class Main {

     public static void main(String args[]) {
          Vehicle.Car value1 = Vehicle.Car.CAR1;
          Vehicle.Bike value2 = Vehicle.Bike.BIKE1;
          
          print(evaluateType(value1));
          // Expected Output: Car

          print(evaluateType(value2));
          // Expected Output: Bike
     }
}

Now the use case here is, We have to write the function evaluateType. I was wondering if there is a way to know the type of Enum we are using, if it is of type Bike or Car.

Given the fact the enums are stored as Int in memory, this doesn't seems like it can be done. But looking forward to any suggestion on how this type of situations could be handled.

Thank you.

  • What is the expected output? A `string` with the class-name? A `Type`? Or do you need it in a `switch`? – akop Nov 10 '22 at 10:50
  • Output should be a String with the Class name. I will edit the question to include more details. – Mritunjay Choubey Nov 10 '22 at 10:58
  • @user16320675 → https://stackoverflow.com/questions/54050291/can-anyone-explain-diagrammatically-how-enum-is-stored-in-memory Looking at this answer I understood that they are stored as Int. – Mritunjay Choubey Nov 10 '22 at 13:23

2 Answers2

0

You can attach values to enum, you don't need to stick with basic enum :

public enum Element {
    H("Hydrogen"),
    HE("Helium"),
    // ...
    NE("Neon");

    public final String label;

    private Element(String label) {
        this.label = label;
    }
}

source : https://www.baeldung.com/java-enum-values

Tristan
  • 8,733
  • 7
  • 48
  • 96
  • I understand the recommendation here, but this doesn't solve this problem. To give you an example. Let's assume there is One More Enums `NobleGas` . Now, the use case here is, to find a way to differentiate if the current element `A` is Element or NobleGas. Hope this provides some more clarity about the ask here. – Mritunjay Choubey Nov 10 '22 at 10:57
  • I can see you've completely rewritten your initial question. It makes a bit more sense what you are trying to do, and it's very easy like shown in the answer below me. – Tristan Nov 10 '22 at 15:17
0

Not sure if that is what you want but:

public class BaseClass {


   public enum PropertyTypeString {
      STRING1,
      STRING2,
      STRING3,
      STRING4,
      STRING5,
   }


   public enum PropertyTypeInt {
     INT1,
     INT2,
     INT3
  }


}


public class Main {

     public static void main(String args[]) {
          BaseClass.PropertyTypeString value = BaseClass.PropertyTypeString.STRING1;
          BaseClass.PropertyTypeInt value_2 = BaseClass.PropertyTypeInt.INT1;
          
          evaluateType(value);
          evaluateType(value_2);
     }

  public static void evaluateType(Enum value)
  {
      System.out.println(value.getClass());
  }
}
Foxy
  • 55
  • 7