Java.lang.Enum.toString() Method

Advertisements


Description

The java.lang.Enum.toString() method returns the name of this enum constant, as contained in the declaration.

Declaration

Following is the declaration for java.lang.Enum.toString() method

public String toString()

Parameters

  • NA

Return Value

This method returns the name of this enum constant.

Exception

  • NA

Example

The following example shows the usage of java.lang.Enum.toString() method.

package com.tutorialspoint;

import java.lang.*;

// enum showing programming languages
enum Language {
   C, Java, PHP;
}

public class EnumDemo {

   public static void main(String args[]) {
       
     // returns the name of this enum constant
     System.out.println("Programming in " + Language.C.toString());       
     System.out.println("Programming in " + Language.Java.toString());  
     System.out.println("Programming in " + Language.PHP.toString());     
   }
} 

Let us compile and run the above program, this will produce the following result:

Programming in C
Programming in Java
Programming in PHP



Advertisements
Advertisements