Rads
Rads

Reputation: 53

Enum syntax explanation

Can anyone explain this Syntax -

public abstract class Enum<E extends Enum<E>>

Why does this let us declare Enum as ?

Public enum XYZ {…}

Upvotes: 0

Views: 68

Answers (1)

Andy Turner
Andy Turner

Reputation: 140318

Why does this let us declare Enum as ?

It doesn't. The syntax for declaring enums is defined in the language spec.

Enum<E extends Enum<E>> is a self-bounded generic type, meaning there is a type variable, E, which allows you to say that a method in an enum returns "the self type".

Looking at the Javadoc, the only method which actually uses E is Class<E> getDeclaringClass(). Having E allows the return value on enum MyEnum to be a Class<MyEnum>.

It's also used e.g. in Comparable, where you use it to say that the compareTo method should only accept a parameter "of the same type".

Upvotes: 1

Related Questions