Enum with values in Java
In this post, you will learn about the enum with values in the Java programming language.
The Enum in Java is a data type which contains a fixed set of constants. The enum keyword is used to create an enum in Java and separate the constants with a comma. This keyword has been introduced since Java 5. The enum in Java is more powerful than the other programming languages. As, we can define an enum either inside the class or outside the class. This feature is not available in c/c++ programming.
An enum type is a special data type that enables a variable to be a set of predefined constants. It can be used for directions (NORTH, SOUTH, EAST, and WEST), days of the week, seasons, etc. As per the Java naming conventions, all the constants should be in capital letters. We can also add values to these constants as-
enum <name> {
<enum_constant_1("value_1")>,
<enum_constant_2("value_2")>,
<enum_constant_3("value_3")>,
...
<enum_constant_n("value_n")>
}
Creating Enum with values
In the given Java program, we are defining an enum with the name Directions and declaring four constants representing the direction names with their codes as values.
enum Directions{
//Constants with values
East("E"),
West("W"),
North("N"),
South("S");
//Instance variable
private String code;
//Constructor to initialize the instance variable
Directions(String code) {
this.code= code;
}
public String getCode() {
return this.code;
}
}
public class EnumTest{
public static void main(String args[]) {
Directions dir[] = Directions.values();
for(Directions d: dir) {
System.out.println("Direction code of "+d+": "+d.getCode());
}
}
}
Output of the above code:
Direction code of East: E
Direction code of West: W
Direction code of North: N
Direction code of South: S
Creating Enum with two values
The given enum contains fruit quantity and their respective price. Quantity and price are passed to the enum constructor for each enum constant.
enum Fruits {
APPLE (5, 120),
GUAVA (4, 110),
ORANGE (7, 90),
KIWI (9, 150);
// declaring variables for getting values
public final double quantity; // (in kg)
public final double price;
Fruits(double quantity, double price) {
this.quantity = quantity;
this.price = price;
}
public double quantity() { return quantity; }
public double price() { return price; }
// getter method
public double totalPrice(double quantity, double price) {
return quantity * price;
}
}
public class FruitsProgram {
public static void main(String[] args) {
for (Fruits ft : Fruits.values())
System.out.printf("Total price of %s is %f%n",
ft, ft.totalPrice(ft.quantity,ft.price));
}
}
Output of the above code:
Total price of APPLE is 600.000000
Total price of GUAVAis 440.000000
Total price of ORANGE is 630.000000
Total price of KIWIis 1350.000000
Related Articles
Enum from string JavaJava enum
Sort array in ascending order Java
String reverse in Java
Count vowels in a string Java
Java compare two strings
Java string split multiple delimiters
Char array to string Java
Java find largest of three numbers
Vowel and Consonant program in Java
Star pattern programs in Java
Number pattern programs in Java
Java program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java