DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • How To Get and Set PDF Form Fields in Java
  • Micro Frontends for Quarkus Microservices
  • Building AI Applications With Java and Gradle
  • Demystifying Project Loom: A Guide to Lightweight Threads in Java

Trending

  • Build Quicker With Zipper: Building a Ping Pong Ranking App Using TypeScript Functions
  • How To Verify Database Connection From a Spring Boot Application
  • Best Practices for Developing Cloud Applications
  • Choosing the Appropriate AWS Load Balancer: ALB vs. NLB
  1. DZone
  2. Coding
  3. Java
  4. Enum - Answer for Constants in Java

Enum - Answer for Constants in Java

Roshan Thomas user avatar by
Roshan Thomas
·
Jan. 31, 15 · Interview
Like (0)
Save
Tweet
Share
53.18K Views

Join the DZone community and get the full member experience.

Join For Free

Enum is a java keyword and very useful in our everyday programming. We use Enum for a variable to be set of a predefined constant value.

Before Java 1.5 we used public static final keywords to represent constants. Enum was introduced in Java 1.5 and since then we use it represent constants or string name for a variable. Also Enums are thread safe by default.

public enum Status {
        COMPLETED(7), FAILED(9), STARTED(3), IN_PROGRESS(6);
        
        private int statusValue;

        private Status (int statusValue) {
                this. statusValue = statusValue;
        }
 
}

Here Status is the Enum and COMPLETED, FAILED, STARTED, IN_PROGRESS are Enum contant values. Enum contastants are static and final by default and cannot be changed after once it is created. Also Enums can be used in Switch statements like int and char data types.

We can define values for enum constants at the time of creation, but then we must add a constructor and a field variable for this enum. Enum constructor must be always private. If we use any other modifier, compiler will throw an error.

  • Enums can be used in Switch case statements like shown below
Status status = Status.COMPLETED;
	switch (status) {
            case STARTED:
                    System.out.println("Application started");
                    break;
            case FAILED:
                    System.out.println("Application failed");
                    break;
            case COMPLETED:
                    System.out.println("Application completed");
                    break;
            default:
		         throw new IllegalArgumentException("Invalid Invocation");
}
  • Enums can implement any interface and override methods
  • Enum in java implicitly implement both Serializable and Comparable interface


public enum Status implements Runnable  {
        COMPLETED(7), FAILED(9), STARTED(3), IN_PROGRESS(6);
        
        private int statusValue;

        private Status (int statusValue) {
                this. statusValue = statusValue;
        }
        
        @Override
        public void run()
        {
        	System.out.println("Within run method");
        }

 
}
  • Abstact methods can be implemented in Enums can provide different implementations for different Enum constants
  • We can create a list directly from the Enum constants using the values() method


List<PodSchdEnumConstants> al = Arrays.asList(PodSchdEnumConstants.values());
Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • How To Get and Set PDF Form Fields in Java
  • Micro Frontends for Quarkus Microservices
  • Building AI Applications With Java and Gradle
  • Demystifying Project Loom: A Guide to Lightweight Threads in Java

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: