
How can we use enum data type in java program?

Example
enum Fruits {
APPLE(10), ORANGE(3), MANGO(5), LICHHI(10);
Fruits(int quantity) {
this.quantity = quantity;
}
int quantity;
public int getQuantity() {
return quantity;
}
}
public class FruitsExample {
Fruits fruits;
public static void main(String[] args) {
FruitsExample fruitsExample = new FruitsExample();
fruitsExample.fruits = Fruits.APPLE;
System.out.print(fruitsExample.fruits);
System.out.print("(" + fruitsExample.fruits.getQuantity() + ")");
}
}
Result Display
APPLE(10)
Description:- In this example we are using enum data type. Here we have defined Fruits as enum type by using the enum keyword. Now we will set some constant values APPLE, ORANGE etc in Fruits. Next define variable and its getter method. Now next step to declare a class named FruitsExample, having main method. We have create a object.now next step to call method.

Example
enum Fruits {
APPLE(10), ORANGE(3), MANGO(5), LICHHI(10);
Fruits(int quantity) {
this.quantity = quantity;
}
int quantity;
public int getQuantity() {
return quantity;
}
}
public class FruitsExample {
Fruits fruits;
public static void main(String[] args) {
FruitsExample fruitsExample = new FruitsExample();
fruitsExample.fruits = Fruits.APPLE;
System.out.print(fruitsExample.fruits);
System.out.print("(" + fruitsExample.fruits.getQuantity() + ")");
}
}
Result Display
APPLE(10)
Description:- In this example we are using enum data type. Here we have defined Fruits as enum type by using the enum keyword. Now we will set some constant values APPLE, ORANGE etc in Fruits. Next define variable and its getter method. Now next step to declare a class named FruitsExample, having main method. We have create a object.now next step to call method.
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.