⬅ Previous Topic
Java Singleton ClassNext Topic ⮕
Reflection in Java⬅ Previous Topic
Java Singleton ClassNext Topic ⮕
Reflection in JavaEnums, short for enumerations, are a special data type in Java used to define collections of constants. Instead of declaring a bunch of final static
variables, you can group related constants under an enum
.
Think of an enum as a predefined set of choices that a variable can take—much like a drop-down list in a form.
enum Direction {
NORTH, SOUTH, EAST, WEST
}
public class EnumDemo {
public static void main(String[] args) {
Direction dir = Direction.NORTH;
System.out.println("The direction is: " + dir);
}
}
The direction is: NORTH
Enums work smoothly with switch
statements for better control flow.
public class SwitchEnum {
public static void main(String[] args) {
Direction dir = Direction.SOUTH;
switch (dir) {
case NORTH:
System.out.println("Going up!");
break;
case SOUTH:
System.out.println("Going down!");
break;
case EAST:
System.out.println("Turning right!");
break;
case WEST:
System.out.println("Turning left!");
break;
}
}
}
Going down!
Yes, enums in Java are much more powerful than in many other languages. You can add fields, constructors, and even methods.
enum Planet {
EARTH(5.97), MARS(0.642), JUPITER(1898);
private final double mass; // in 10^24 kg
Planet(double mass) {
this.mass = mass;
}
public double getMass() {
return mass;
}
}
public class EnumWithFields {
public static void main(String[] args) {
for (Planet p : Planet.values()) {
System.out.println(p + " has a mass of " + p.getMass() + " x10^24 kg");
}
}
}
EARTH has a mass of 5.97 x10^24 kg
MARS has a mass of 0.642 x10^24 kg
JUPITER has a mass of 1898.0 x10^24 kg
values()
and valueOf()
These two methods are available by default:
values()
– returns an array of all enum constants.valueOf(String name)
– returns the enum constant with the given name.public class EnumBuiltInMethods {
public static void main(String[] args) {
// List all directions
for (Direction d : Direction.values()) {
System.out.println(d);
}
// Convert string to enum
Direction selected = Direction.valueOf("EAST");
System.out.println("Selected: " + selected);
}
}
NORTH
SOUTH
EAST
WEST
Selected: EAST
Each enum constant can have its own implementation of a method using abstract methods.
enum Operation {
ADD {
public int apply(int a, int b) {
return a + b;
}
},
SUBTRACT {
public int apply(int a, int b) {
return a - b;
}
};
public abstract int apply(int a, int b);
}
public class EnumAbstractMethod {
public static void main(String[] args) {
System.out.println("Addition: " + Operation.ADD.apply(10, 5));
System.out.println("Subtraction: " + Operation.SUBTRACT.apply(10, 5));
}
}
Addition: 15
Subtraction: 5
==
and equals()
interchangeably—though ==
is safe for enums.enum
in Java?enum Direction { NORTH, SOUTH, EAST, WEST }
public class Demo {
public static void main(String[] args) {
Direction d = Direction.SOUTH;
System.out.println(d);
}
}
enum Planet {
EARTH(5.97), MARS(0.642);
private final double mass;
Planet(double mass) {
this.mass = mass;
}
public double getMass() {
return mass;
}
}
What is the significance of using getMass()
here?switch
statements.⬅ Previous Topic
Java Singleton ClassNext Topic ⮕
Reflection in JavaYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.