Java Enum name() Method

The name() method of Enum class returns the name of this enum constant same as declared in its enum declaration.

The toString() method is mostly used by programmers as it might return a more easy to use name as compared to the name() method.

Syntax

Return Value

The name() method returns the name of this enum.

Example 1

Output:

1. Cow is in class com.javaTpoint.Enum_nameMethodExample1$Herbivores enum class
2. Leopard is in class com.javaTpoint.Enum_nameMethodExample1$Carnivores enum class
3. Bear is in class com.javaTpoint.Enum_nameMethodExample1$Omnivores enumclas

Example 2

Output:

Types of Triangles:
Right_Angle_Triangle is a triangle in which one angle is 90 degree.
Obtuse_Angled_Trianlge is a triangle in which one angle is greater than 90 degree.
Acutee_Angled_Trianlge is a triangle in which one angle is less than 90 degree.

Example 3

Output:

Circles has 0 sides.
Triangles has 3 sides.
Quadrilaterals has 4 sides.
Pentagon has 5 sides.