Java Enums Class

An Enum(or Enumeration) is a set of constant values (Amazon, Google, Facebook, Microsoft). We can create an enum using a keyword enum.

Code:

Java Code

/*package whatever //do not write package name here */ 
import java.io.*; 
enum topmnc{ 
AMAZON, 
GOOGLE, 
FACEBOOOK, 
MICROSOFT; 
} 
class Main { 
public static void main (String[] args) { 
System.out.println(topmnc.MICROSOFT); //accessing its elements 
} 
} 

Output: Microsoft

Enum Class in Java

In Java, It is a special type of class that contains methods, objects, fields, etc. like regular classes. Whenever we create an Enum class, the compiler creates an object of each constant of the Enum. By default, All the Enum constants are public static final. 

Code:

Java Code

import java.io.*; 
enum topmnc{ 
AMAZON, 
GOOGLE, 
FACEBOOOK, 
MICROSOFT; 
public String getname(){ 
switch(this){ 
case AMAZON: 
return "Amazon is owned by Jeff Bezos"; 
case GOOGLE: 
return "Sundar Pichai is the CEO of Google"; 
case FACEBOOOK: 
return "Mark Zuckerberg invented facebook"; 
case MICROSOFT: 
return "Satya Nadella is the CEO of Microsoft"; 
default: 
return null;
} 
} 
} 
class Main { 
public static void main (String[] args) { 
System.out.println(topmnc.MICROSOFT.getname()); //accessing its elements 
} 
} 

Output:

Satya Nadella is the CEO of Microsoft

WHY USE ENUMS IN JAVA – 

In Java, enums are used to replace integer constants. 

Enums come with many functions, and it serves as a class, with objects, and all the functions of a class can be performed m instead of an integer variable. Using enums, make code more readable and intuitive. 

An enum also provides compile-time type safety. 

METHODS OF ENUM CLASS IN JAVA – 

There are some predefined methods already available to use – 

  • Java Enum ordinal() – 

It returns the position of an Enum Constant. 

Syntax- 

ordinal(enum_constant) //return the position of the constant declared in the enum.
  • Enum compareTo() – 

It compares the Enum Constants based on their ordinal value.

Syntax:

enum_class_name.enum_constant1.comapreTo(enum_class_name.enum_constant2 ) //enum_constant1 and enum_constant2 are the two constants whose values need to be compared. 
// This returns ordinal(enum_constant1) - ordinal(enum_constant2). 
  • Enum to String() – 

It represents the string representation of the enum constants. 

enum_class_name.toString()
  • Enum name() – 

It returns the defined name of the enum constant in string form. 

Syntax – 

name(enum_class_name)
  • Java Enum valueOf() – 

It takes a string and returns an enum constant having the same name.

Syntax – 

enum_class_name.valueOf(“string”)
  • Enum Values() – 

It returns an array containing all the enum constant values.

Special thanks to Ishita Dhiman for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this articleIf you want to suggest any improvement/correction in this article please mail us at [email protected]