Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What would be different in Java if Enum declaration didn't have the recursive part

Please see Java Enum definition and Why in java enum is declared as Enum<E extends Enum<E>> for general discussion. Here I would like to learn what exactly would be broken (not typesafe anymore, or requiring additional casts etc) if Enum class was defined as

public class Enum<E extends Enum> 

I'm using this code for testing my ideas:

interface MyComparable<T> {
    int myCompare(T o);
}

class MyEnum<E extends MyEnum> implements MyComparable<E> {
    public int myCompare(E o) { return -1; }
}

class FirstEnum extends MyEnum<FirstEnum> {}

class SecondEnum extends MyEnum<SecondEnum> {}

With it I wasn't able to find any benefits in this exact case.

PS. the fact that I'm not allowed to do

class ThirdEnum extends MyEnum<SecondEnum> {}

when MyEnum is defined with recursion is
a) not relevant, because with real enums you are not allowed to do that just because you can't extend enum yourself
b) not true - pls try it in a compiler and see that it in fact is able to compile w/o any errors

PPS. I'm more and more inclined to believe that the correct answer here would be "nothing would change if you remove the recursive part" - but I just can't believe that.

like image 233
atamur Avatar asked Jun 18 '10 07:06

atamur


People also ask

What is the purpose of enum in java?

An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it. Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week.

Can enum have static methods?

Yes, the method isCdValue has to be static here. An enum is a special kind of class. An enum constant defines an instance of the enum type. An enum type has no instances other than those defined by its enum constants.

Should enums be capitalized?

Enums are a type and the enum name should start with a capital. Enum members are constants and their text should be all-uppercase.

Can we have enum inside enum in Java?

yes, but you need to know that. no way to easily iterate through all of the enum instances.


1 Answers

Well, first of all it would complain about using the raw type, but you could do:

public class Enum<E extends Enum<?>>

for the same effect.

Also, with this type of generic you could do something like:

class FirstEnum extends MyEnum<SecondEnum> {
}

class SecondEnum extends MyEnum<FirstEnum> {
}

which to me seems like it could lead to a lot of trouble. More exactly you can't compare an enum of type FirstEnum with an enum of the same type, you have to compare it with an enum of the other type, which is really troublesome if you have a List<FirstEnum> that you want sorted. The example will not compile if i set E instead o ? since SecondEnum is not of the type E extends MyEnum<E> (this would lead to circular inheritance). It will work if FirstEnum extends MyEnum<FirstEnum> though (which would mean that SecondEnum is a child class of FirstEnum - normal hierarchical inheritance).

like image 173
Andrei Fierbinteanu Avatar answered Sep 21 '22 18:09

Andrei Fierbinteanu