0% found this document useful (0 votes)
2 views

Java - Enum Class

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Java - Enum Class

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

01/02/2024, 23:53 Java - Enum Class

Java - Enum Class

Java Enum Class


Java Enum class is the common base class for all enumeration types. It is a special
class that contains a group of pre-defined constant values that are known at the
compile-time itself.

Java Enum Class Declaration


Following is the declaration for java.lang.Enum class −

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


extends Object
implements Comparable<E>, Serializable

Java Enum Class Constructors

Java Enum Class Methods

Methods Inherited
This class inherits methods from the following classes −

java.lang.Object

Java Enum Class Example


Following example showcases the usage of enum in if and switch statements.

package com.tutorialspoint;
public class EnumDemo {
public static void main(String args[]) {

https://www.tutorialspoint.com/java/java_lang_enum.htm 1/2
01/02/2024, 23:53 Java - Enum Class

//print an Enum
System.out.println(Mobile.Motorola);

Mobile mobile = Mobile.Samsung;


//Usage in IF statment
if(mobile == Mobile.Samsung) {
System.out.println("Matched");
}
//Usage in Swith statment
switch(mobile) {
case Samsung:
System.out.println("Samsung");
break;
case Nokia:
System.out.println("Nokia");
break;
case Motorola:
System.out.println("Motorola");
}
}
}
enum Mobile {
Samsung,
Nokia,
Motorola
}

Output

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

Motorola
Matched
Samsung

https://www.tutorialspoint.com/java/java_lang_enum.htm 2/2

You might also like