Isn't there a name collision when enum has the same name in the switch statement?

Introduction

In Java switch statement, when enum is used as an expression, qualified name is not given. It was strange that the compilation passed when there were two or more same constants, so I checked it. First of all, no modification is required, so you can use it as it is without worrying about it.

When there is one enum

Suppose you have an enum like this.

FoodType.java


public enum FoodType {
    RICE,
    BREAD,
    OTHER,
}

Main.java


import lombok.AllArgsConstructor;

public class Main {
    public static void main(String[] args) {
        Food food = new Food(FoodType.RICE);
        switch (food.foodType){
        case RICE:
            System.out.println("Solder");
            break;
        case BREAD:
            System.out.println("It's bread");
            break;

        default:
            System.out.println("Other");
            break;
        }
    }
}

@AllArgsConstructor
class Food {
    FoodType foodType;
}

On the Main side, the enum (FoodType) is branched by the switch statement. In this case, write the case as RICE instead of FoodType.RICE. (Why RICE is good will be explained later.)

When there are two enums

Let's add an enum to the code above. Suppose you want to subdivide the rice category.

RiceType


public enum RiceType {
    RICE,
    FriedRice,
}

Main also changed.

Main.java


import lombok.AllArgsConstructor;

public class Main {
    public static void main(String[] args) {
        Food food = new Food(FoodType.RICE,RiceType.RICE);
        switch (food.foodType){
            case RICE:
                switch (food.riceType){
                    case RICE:
                        System.out.println("Solder");
                        break;
                    case FriedRice:
                        System.out.println("It's fried rice");
                        break;
                }
                break;
            case BREAD:
                System.out.println("It's bread");
                break;

            default:
                System.out.println("Other");
                break;
        }
    }
}
@AllArgsConstructor
class Food {
    FoodType foodType;
    RiceType riceType;
}

Why names don't collide

The above code will compile. This is correct code, but I was wondering if case RICE: and case RICE: would compile without conflict in the following parts of Main: Also, import is not used because all the code is included.

      switch (food.foodType){
            case RICE:
                switch (food.riceType){
                    case RICE: 

Qualifies at compile time

Upon examination, the class names are now qualified at compile time, so the names don't seem to conflict. It's a bit old, but you can see the result of decompiling enums in the "Implementing a switch" section of the following site. http://www.ne.jp/asahi/hishidama/home/tech/java/enum.html#h2_switch

Summary

It was a mechanism that was qualified at compile time.

Recommended Posts

Isn't there a name collision when enum has the same name in the switch statement?
Java learning_Behavior when there is a method with the same name as a field with the same name in two classes that have an inheritance relationship
How to reference a column when overriding the column name method in ActiveRecord
Be careful when setting the class name when creating a document-based app in Swift
[Java] When putting a character string in the case of a switch statement, it is necessary to make it a constant expression
[Rails Tutorial Chapter 2] What to do when you make a mistake in the column name
wsimport error handling (A class / interface with the same name "xxx" is already in use)
[Head panic] Priority when variables / methods with the same name are used in inherited classes
How to add the same Indexes in a nested array
I was confused because there was a split in the Array