CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > Java > Enum in Java

Java Tutorial

  • Introduction
    • What is Java
    • History Of Java
    • Install Java
    • What is JVM
    • JDK vs JRE vs JVM
    • Java Bytecode
    • OOP vs POP
    • Compile and Run Java
  • Tokens, Expressions and Control Structures
    • Primitive data types
    • Integers
    • Floating Points
    • Characters
    • Booleans
    • User Defined Data Type
    • Declarations
    • Constants
    • Identifiers
    • Literals
    • Type Conversion and Casting
    • Variables
    • Default Variable Initialization
    • Command Line Arguments
    • Arrays of Primitive Types
    • Comment Syntax
    • Garbage Collection
    • Expressions
    • Operators
    • Arithmetic Operator
    • Bitwise and Shift Operator
    • Comparison or Relational Operators
    • Logical Operators
    • Assignment Operators
    • Ternary Operator
    • Increment and Decrement Operator
    • Control Statements
  • OOP Concepts
    • Class and Object
    • Create Class Instance
    • Method
    • Abstraction
    • Encapsulation
    • this keyword
    • Constructor
    • Pass by Value
    • Access Modifier/Control
    • Polymorphism
    • Method Overloading vs Method Overriding
    • Recursion
    • Nested and Inner Class
  • Inheritance and Packaging
    • Inheritance
    • extends Keyword
    • super Keyword
    • Object Class
    • Abstract class
    • Final Class
    • Java Package
    • Interface
  • Handling Error/ Exceptions
    • What is Exception
    • Exception Handling Keywords
    • Common Java Errors
    • User Defined Exception
    • Throwing and re-throwing Exception
    • finally Block
  • Strings
    • Java String Tutorial
  • Threads
    • Introduction
    • Create Thread
    • Thread Lifecycle
    • Thread Priority
    • Thread Synchronization
    • Inner Thread Communication
    • Thread Deadlock
  • IO and Streams
    • java.io Package
    • Files and Directories
    • Byte Stream
    • Character Stream
    • Console Input and Output
    • Serializable and Deserializable
  • Core Packages
    • java.lang Package
    • Math
    • Wrapper Classes
    • java.lang.Number
    • Double
    • Float
    • Integers
    • java.lang.Byte
    • java.lang.Short
    • java.lang.Long
    • java.lang.Character
    • java.lang.Boolean
    • java.util package
    • Vector Class
    • Stack Class
    • Dictionary Class
    • Hashtable
    • Enumeration or Enum
    • Generate Random Number
  • Holding Collection of Data
    • Arrays
    • Map
    • List
    • Set
    • Collection Interface
    • Collections Class
    • ArrayList
    • HashSet
    • TreeSet
    • Comparator
  • Java Bean
    • What is Java Bean
    • Advantages and Disadvantages of Java Bean
    • Java Beans API
    • Introspection
    • Java Bean Properties
    • Bound and Constrained Properties
    • BeanInfo Interface
    • Customizers
    • Java Beans Persistence
    • BeanDescriptor
  • Home

Enum in Java

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Last updated: July 1, 2026 ยท 3 min read ยท 0 Comments
Share: in X

Enumerations, or enums for short, are a powerful and convenient way to define a set of named constants in Java. Enum in Java are essentially a special data type that allows a variable to be a set of predefined values. This makes easier to write and maintain code that uses these values. In this post, we will learn what is enum type in Java with example.

Table of Contents

Create Enum in Java
Enum as a variable
Using enum in switch case
Create method and variable in enum
FAQs about Enum in Java

Create Enum in Java

To define an enum, we use the enum keyword followed by a list of values enclosed in curly braces. For example:

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

Here, we have defined an enum called Day that contains seven values: SUNDAY, MONDAY, etc. These values are called enum constants.

Enum as a variable

We can use an enum just like any other data type in Java. For example, we can declare a variable of type Day and assign it one of the enum constants:

Day today = Day.MONDAY;

Using enum in switch case

We can also use enums in switch statements, which can make our code more readable and easier to maintain. For example:

switch (today) {
    case SUNDAY:
        System.out.println("Today is Sunday");
        break;
    case MONDAY:
        System.out.println("Today is Monday");
        break;
    // ...
}

Create method and variable in enum

In addition to the constants defined in the enum, we can also define fields and methods inside the enum. For example:

public enum Day {
    SUNDAY("Sunday"), MONDAY("Monday"), TUESDAY("Tuesday"), WEDNESDAY("Wednesday"), THURSDAY("Thursday"), FRIDAY("Friday"), SATURDAY("Saturday");
    
    private String fullName;
    
    private Day(String fullName) {
        this.fullName = fullName;
    }
    
    public String getFullName() {
        return fullName;
    }
}

Here, we have added a field called fullName and a method called getFullName to the Day enum. The fullName field is initialized in the constructor, which is called whenever an enum constant is created. The getFullName method simply returns the value of the fullName field.

FAQs about Enum in Java

Can enums have methods?

Yes, enums can have constructors, instance variables, and methods just like regular classes.

Are enums part of the Java Collections Framework?

No, enums are not part of the Java Collections Framework. They are a separate type of data structure.

Can I compare enums?

Yes, enums can be compared using the == operator or the equals() method.

Can I use enums as keys in HashMaps?

Yes, enums can be used as keys in HashMaps and other data structures.

Can I add new enum constants at runtime?

No, enums are defined at compile-time and cannot be modified or extended at runtime.

Are enums type-safe?

Yes, enums provide type safety, as they restrict the possible values to the ones defined in the enum.

Related Posts:

  • Control Statements in Java
  • Constant in Java
  • final Keyword in Java
  • Switch Statement with Strings in Java
  • Identifiers in Java
  • MySQL Commands for Developers
Tags:java
Was this article helpful?
โ† Previous ArticleJDesktopPane and JInternalFrame in Java Swing
Next Article โ†’Using Color in Java Swing

Recent Posts

  • How to implement Passwordless Authentication in Spring Boot: A Step-by-Step Guide
  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Linkedin

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
ยฉ 2026 CoderSathi. All rights reserved. Privacy Policy ยท Sitemap