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 [Link] 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 −
[Link]
Java Enum Class Example
Following example showcases the usage of enum in if and switch statements.
package [Link];
public class EnumDemo {
public static void main(String args[]) {
[Link] 1/2
01/02/2024, 23:53 Java - Enum Class
//print an Enum
[Link]([Link]);
Mobile mobile = [Link];
//Usage in IF statment
if(mobile == [Link]) {
[Link]("Matched");
}
//Usage in Swith statment
switch(mobile) {
case Samsung:
[Link]("Samsung");
break;
case Nokia:
[Link]("Nokia");
break;
case Motorola:
[Link]("Motorola");
}
}
}
enum Mobile {
Samsung,
Nokia,
Motorola
}
Output
Let us compile and run the above program, this will produce the following result −
Motorola
Matched
Samsung
[Link] 2/2