Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Enum - Answer for Constants in Java

DZone's Guide to

Enum - Answer for Constants in Java

· Java Zone ·
Free Resource

Verify, standardize, and correct the Big 4 + more – name, email, phone and global addresses – try our Data Quality APIs now at Melissa Developer Portal!

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());

Developers! Quickly and easily gain access to the tools and information you need! Explore, test and combine our data quality APIs at Melissa Developer Portal – home to tools that save time and boost revenue. Our APIs verify, standardize, and correct the Big 4 + more – name, email, phone and global addresses – to ensure accurate delivery, prevent blacklisting and identify risks in real-time.

Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}