Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is `enum of enum of enum..` allowed?

Tags:

java

syntax

enums

I cannot understand why this even compile. I've tried with different formats and they all seem to work..

why is it legal to have an enum of enum of enum of..?

interface I {

    enum E implements I {
        VAL;
    }

    class Test {
        I.E         f1 = I.E.VAL;
        I.E.E       f2 = I.E.VAL;
        I.E.E.E     f3 = I.E.VAL;
        I.E.E.E.E.E f4 = I.E.VAL;

        I.E v1 = I.E.VAL;
        I.E v2 = I.E.E.VAL;
        I.E v3 = I.E.E.E.E.E.E.VAL;
        I.E v4 = I.E.E.E.E.E.E.E.E.E.E.VAL;
    }
}

My IDE reports it compiles just fine, although I.E.E does not make sense to me.

like image 514
eridal Avatar asked Sep 14 '14 20:09

eridal


People also ask

Can we have enum inside enum in Java?

Yes, we can define an enumeration inside a class. You can retrieve the values in an enumeration using the values() method.

Why are enums thread safe?

This technique is absolutely thread-safe. An enum value is guaranteed to only be initialized once, ever, by a single thread, before it is used.

What is the purpose of enums enumerations in Java?

The main objective of enum is to define our own data types(Enumerated Data Types). Declaration of enum in Java: Enum declaration can be done outside a Class or inside a Class but not inside a Method.

Why do we use enumerations?

Enumerations make for clearer and more readable code, particularly when meaningful names are used. The benefits of using enumerations include: Reduces errors caused by transposing or mistyping numbers. Makes it easy to change values in the future.


1 Answers

Your I interface contains an enum type named E.

This type implements that same I interface, so it inherits everything that that interface contains.
This includes the E type itself.

In other words, I.E.E is accessing I.E as inherited by E from the outer I.

like image 118
SLaks Avatar answered Sep 30 '22 13:09

SLaks