Java Singleton Example With Enum
This article describes the following for Java Enum with examples:
- What is enum in Java ?
- How to declare/create an enum ?
- How to access enums in a loop ?
- How to access enum in a switch statement?
- How to compare enums ?
- enums with properties and methods
- enum constants with body and abstract methods
- Singleton implementation using enum
What is enum in Java ?
An enum type is a type whose fields consist of a fixed set of constant values.
enum types were introduced in Java language in release 1.5.
How to create an enum ?
The following code is a simple example of creating an enum Color that has values RED, BLUE or GREEN.
public enum Color { RED, BLUE, GREEN }
The following code shows how we can use the enum we just created.
We can use color constant RED as Color.RED
Color color = Color.RED; System.out.println("color.name(): " + color.name()); System.out.println("color.ordinal(): " + color.ordinal()); System.out.println("color.toString(): " + color.toString());
Output:
color.name(): RED
color.ordinal(): 0
color.toString(): RED
How to access enum in For-Each loop ?
public class EnumDemo { public static void main(String[] args) { for (Color color : Color.values()) System.out.println(color); } }
Output:
RED
BLUE
GREEN
How to access enum in Switch Statement ?
Color color = Color.RED; switch(color){ case RED: System.out.println("RED"); break; case GREEN: System.out.println("GREEN"); break; case BLUE: System.out.println("BLUE"); break; default: System.out.println("Other Color"); }
Output:
RED
How to compare enums ?
When testing for equality, both the equals() method and == perform reference comparison.
However, == is recommended for comparison to take advantage of compile-time type safety.
Performing equals() comparison with a String parameter, for example, may allow the error to go unnoticed; it will compile, but it will always return false.
Conversely, attempting to compare an enum with a String using the == comparison would result in an error at compile
time.
Here is an example:
System.out.println("color.equals()" + color.equals(Color.RED)); // true System.out.println("color.equals()" + color.equals("RED")); // false System.out.println("color == Color.RED " + (color == Color.RED)); //true System.out.println("color == RED" + (color == "RED")); //Compilation error
Also, Enum class implements the java.lang.Comparable interface. So, we can use the compareTo() method
to determine if one enum constant is declared before or after another enum constant.
Color color = Color.RED; Color anotherColor = Color.GREEN; if(color.compareTo(anotherColor)< 0) System.out.println(color + " declared before " + anotherColor); else System.out.println(color + " declared after " + anotherColor);
Output:
RED declared before GREEN
enums as Classes
We can define attributes, methods, and constructors just like in a standard class.
An enum cannot have public constructor though.. only private constructor is allowed in an enum.
public enum Color { RED(1), BLUE(2), GREEN(3); private int value; private Color(int value) { this.value = value; } public int getValue() { return value; } }
enum constants with Body and abstract methods
We can associate a different body to each enum constant. The body can have fields and methods.
Also, we have declared abstract method getSomeMessage() that each enum member implements. This allows for each enum member to define its own behavior for a given operation.
package com.topjavatutorial; public enum Color { RED(1) { @Override public String getSomeMessage() { return "This is for RED color"; } }, BLUE(2) { @Override public String getSomeMessage() { return "This is for BLUE color"; } }, GREEN(3) { @Override public String getSomeMessage() { return "This is for GREEN color"; } }; private int value; private Color(int value) { this.value = value; } public abstract String getSomeMessage(); public int getValue() { return value; } }
enum as Singleton
Enum constants are instantiated the first time then Enum is referenced. Therefore, Enum is a good candidate for implementing Singleton pattern.
public enum EnumSingleton { INSTANCE; // add sinlgeton methods public void method() { System.out.println("SingleTon Method"); } }
public class TestEnumSingleton { public static void main(String[] args) { EnumSingleTon.INSTANCE.method(); } }
Reference : Singleton Design Pattern in Java
© 2016 – 2018, https:. All rights reserved. On republishing this post, you must provide link to original post
cardenaswhimse1972.blogspot.com
Source: https://www.topjavatutorial.com/java/java-enum-examples/
0 Response to "Java Singleton Example With Enum"
Post a Comment