0

how can I access these fields from a switch in a different class? I need to do a switch case for each LocationType.

@FunctionalInterface
public interface LocationType {
    LocationType UNKNOWN = () -> "Unknown Location";
    LocationType MERGE = () -> "Merge Location";
    LocationType REGULAR = () -> "Regular Location";
    LocationType MULTI = () -> "Multi Location";
    LocationType ZONE = () -> "Zone Location";

    String getName();
}

e.g. what I need to do later on, in another class is this:

switch (locationList) {
    case LocationType.MERGE:
        break;
    case LocationType.MULTI:
        break;
}
4673_j
  • 477
  • 1
  • 6
  • 20
  • Does this answer your question? [enum implementation inside interface - Java](https://stackoverflow.com/questions/15318796/enum-implementation-inside-interface-java) – Phaelax z Apr 08 '22 at 12:26
  • Almost, the issue is that the enum types are the same as the interface, if you'll notice, the interface name is the same as each field declared, as it's of the same type. – 4673_j Apr 08 '22 at 12:32
  • 3
    Code in the question needs to be represented as a text. Please edit your question, replacing the images of the code with the actual code, and apply the appropriate formatting (by enclosing the code into triple backticks **```**, or by pressing the button `{}`). – Alexander Ivanchenko Apr 08 '22 at 15:36
  • 2
    You don’t have an enum type. You have a functional interface. Then, you are adding even more confusion by suddenly talking about a *list*. Further, as said by others, you should post your code as text. Doing copy&paste with text is even simpler than making and uploading a screenshot. – Holger Apr 12 '22 at 16:14
  • 1
    It’s not our duty to write your code. You are the one who has a question. You can not expect a solution if you fail to describe your actual problem. Is there a semantic difference between your impossible switch over `LocationType` and just doing a switch over the result of `getName()`, like `switch(location.getName()) { case "Unknown Location": … case "Merge Location": … }`? If there is, describe it, as said, no-one can solve your problem when you don’t explain your problem. – Holger Apr 13 '22 at 10:15
  • @Holger, elegant reply; Indeed, that would be the answer, however I was hopping to use the ```.getName()``` in the switch cases, apparently this is a enum/switch limitation, as everything in the interface is a constant, and the switch expects a constant. I was hoping to use: ```LocationType loc = LocationType.MERGE; switch (loc.getName()) { case LocationType.UNKNOWN.getName(): break; case LocationType.MERGE.getName(): break; }``` but then this would not work if the ```case``` is not a known string - same as you mention above. – 4673_j Apr 13 '22 at 17:11
  • 2
    With the current language rules, neither `LocationType.UNKNOWN` nor `LocationType.UNKNOWN.getName()` is a constant. There are long term plans to introduce a kind of constant expressions beyond the current compile-time constant constructs. But it’s still not clear whether it fits your use case, i.e. having an arbitrary `LocationType x = () -> "Unknown Location";`, should this `x` be treated as equal to `LocationType.UNKNOWN` for your `switch`? – Holger Apr 13 '22 at 17:20

1 Answers1

1

Assuming that you want to compare only by the String and not by the number of the enum list here it is a possible solution.

public enum Locations{   
   
     UNKNOWN{
          public String toString() {
            return "Unknown Location";
          } 
      },
     MERGE{
          public String toString() {
            return "Merge Location";
          } 
      },
    REGULAR{
          public String toString() {
            return "Regular Locaton";
          } 
      },
      MULTI{
          public String toString() {
            return "Multi Location";
          } 
      },
    ZONE{

          public String toString() {
            return "Zone Location";
          } 
      }, }
Nikolao
  • 29
  • 1
  • 5