Enum toString() in Java

toString(): This method is available in java.lang.Enum class of Java.

Syntax:

String java.lang.Enum.toString()

This method returns the name of this enum constant, as contained in the declaration.

Parameters: NA

Returns: the name of this enum constant.

Exceptions: NA

Approach

Java

enum Colour {
    RED, GREEN, GRAY, ORANGE
}

public class EnumtoString {
    public static void main(String[] args) {

        System.out.println(Colour.RED.toString());
    }
}

Output:

RED


No comments:

Post a Comment