Java Tutorials
  • Introduction to Java
    • What is Java?
    • History and Features of Java
    • Java Virtual Machine (JVM) and Bytecode
    • Why Java?
  • Setting up Java Development Environment
    • Installing Java Development Kit (JDK)
    • JDK vs JRE
    • Setting up IDE (Eclipse, IntelliJ, NetBeans) or Text Editor (VS Code, Sublime Text)
  • Basic Java
    • First Java Program : Hello World
    • Variable
    • Data Type
    • Constant
    • Date and Format
    • Operator
    • Condition
    • Looping
    • Function
    • Variadic Function
    • Enums
    • Array
    • Collection
    • Exception and Exception Handling
    • Naming Convention
  • Object Oriented Programming (OOP)
    • Classes and Objects
    • Inheritance and Polymorphism
    • Encapsulation and Abstraction
  • File Handling
    • Reading and Writing Binary File
    • Reading and Writing Text File
    • Serialization and Deserialization
  • Multithreading
    • Creating and Running Threads
    • Synchronization
    • Thread Pools and Executors
  • Collections API
    • Sorting and Comparable
    • Searching and Comparator
  • Java Database Connectivity (JDBC)
    • Introduction and Life Cycle
    • Connection to Database (MySQL)
    • Downloading JDBC Drivers for Various Databases
    • Maven and Gradle JDBC Drivers for Various Databases
    • JDBC URL Formats
    • Statement and PreparedStatement
    • CallableStatement
    • Selecting Data using JDBC
    • Inserting Data using JDBC
    • Updating Data using JDBC
    • Deleting Data using JDBC
    • Invoking Function and Stored Procedure using JDBC
  • Lambda
    • Introduction to Lambda Expressions
    • Functional Interface
    • Filtering, Mapping, Reducing
    • Lambda Expressions in Collections
    • Method References
    • Functional Programming Concepts
    • Stream API
    • Error Handling in Lambda Expressions
    • Optional in Functional Programming
    • Parallel Processing with Lambda
    • Functional Programming Patterns
    • Advanced Topics in Lambda Expressions
    • Best Practices and Design Patterns
    • Real-World Use Cases and Examples
Powered by GitBook
On this page
  1. Basic Java

Enums

Enums, short for Enumerations, were introduced in Java 5. They are a special data type used to define collections of constants. Enums are used when a variable can only take one of a predefined set of values. Enums enhance code readability, prevent invalid values, and provide type safety.

Declaration and Usage of Enums:

Declaration:

public enum DaysOfWeek {
    MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}

Usage:

public class EnumExample {

    public static void main(String[] args) {
        DaysOfWeek today = DaysOfWeek.TUESDAY;

        if (today == DaysOfWeek.TUESDAY) {
            System.out.println("It's Tuesday!");
        }

        // Enum in a switch statement
        switch (today) {
            case MONDAY:
                System.out.println("It's Monday!");
                break;
            case TUESDAY:
                System.out.println("It's Tuesday!");
                break;
            // ... other cases ...
            default:
                System.out.println("It's some other day.");
        }

        // Iterating through enum values
        System.out.println("Days of the week:");
        for (DaysOfWeek day : DaysOfWeek.values()) {
            System.out.println(day);
        }
    }
}

Explanation:

  1. Enum Declaration: Enums are declared using the enum keyword. Enum constants are defined in uppercase letters by convention.

  2. Enum Instantiation: Enum constants can be used like any other objects and assigned to variables. For example, DaysOfWeek today = DaysOfWeek.TUESDAY; assigns the TUESDAY constant to the today variable.

  3. Comparisons: Enums are compared using the == operator. You can use enums in if statements and other conditional constructs.

  4. Switch Statements: Enums work well with switch statements, providing a clean and readable way to handle different cases.

  5. Iterating through Enums: The values() method provides an array of all enum constants, allowing you to iterate through them.

Enums can also have constructors, methods, and fields like regular Java classes. Each enum constant can have its own implementations of methods, allowing enums to have unique behaviors for each constant. Enums are powerful tools for modeling sets of related constants in a clear and type-safe manner.

PreviousVariadic FunctionNextArray

Last updated 1 year ago