Day - 11 Enum and learning
// 🔢 What are Enums and Why Should You Use Them?
// Ever needed to represent a fixed set of values — like days of the week,
payment statuses, or user roles?
// That’s where Enums shine! 🌟
// 📌 What is an Enum?
// Short for Enumeration, an enum is a special “type” that defines a collection of
named constants.
// Think of it as a predefined list of allowed values.
// ✅ Why Use Enums?
// ✅ Type Safety – Prevents invalid values
// ✅ Readability – Names over magic numbers/strings
// ✅ Maintainability – Easy to update and extend
// ✅ Built-in Methods – .values(), .ordinal(), .name()
public class enum_orderStatus {
// Create enum (fixed set of values)
enum OrderStatus {
ORDERED,
PACKED,
SHIPPED,
OUT_FOR_DELIVERY,
DELIVERED
}
public static void main(String[] args) {
// Use enum
OrderStatus status = OrderStatus.SHIPPED;
// Print status
System.out.println("Your order status is: " + status);
}
}
asdf
Comments
Post a Comment