Enum


Enum

A special type of data type which is a collection of constants is known as an Enum.

Syntax:

public enum Days{
Sunday,
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday
}

Here, Days is a variable of enum type and is a collection of 7 constants.

enumMap is the map implementation which is sued with enum type and enumSet is the set implementation which is sued with enum type.

The different design patterns which can be implemented using enums are as follows:

Singleton Pattern

Strategy Pattern

Example No.1:

Com.knowledge2life enum Level { Knowledge2life, Knowledge2life1, Knowledge2life2 } public class Main { public static void main(String[] args) { Level myVar = Level.Knowledge2life1; System.out.println(myVar); } }

OUTPUT:

Knowledge2life1

Example No.2:

Com.knowledge2life public class Main { enum Level { Knowledge2life, Knowledge2life1, Knowledge2life2 } public static void main(String[] args) { Level myVar = Level.Knowledge2life2; System.out.println(myVar); } }

OUTPUT:

Knowledge2life2
Knowledge2life