Enumeration or enum in Java

Enumeration :

An enumeration is a list of named constants.
Or
Enumeration special type which contains
Group of named constants.

Enumeration can have constructors, methods and instance variables.


Syntax:

enum Variable_Name 

VALUE_1, VALUE_2, VALUE_3, … 

}
An enumeration is created using the
keyword enum.


Program:

classEnumExample
{
public enum Season { WINTER, SPRING, SUMMER, FALL }
public static void main(String[] args)
{
for (Season s : Season.values())
System.out.println(s);
}
}

Output:
WINTER
SPRING
SUMMER
FALL

The following characteristics make enum a ‘special’ class:
enum constants cannot be overridden 
enum doesn’t support the creation of objects
enum can’t extend other classes enum can implement interfaces like classes.


Explain restrictions on using enum in java?
Each enum declaration declares an enum class with the following restrictions:

1. enum constants are implicitly final, because they declare constants that shouldn’tbe modified.
2. enum constants are implicitly static.
3. Any attempt to create an object of an enum type with operator new results in a
compilation error.
The enum constants can be used anywhere constants can be used, such as in the case labels of switch statements and to control enhanced for statement
4.You cannot define an enum inside a method. If you try to do so it generates a compile time error saying “enum types must not be local”.

5.You can define an enum inside a class.You can have variables and methods inside an enum .

6.It is recommended to write the name of the constants in all capital letters 


program to illustrate working of enumerations.

Example 1:
public enum Level { HIGH, MEDIUM, LOW }


Program:
import java.util.*;
public class EnumDemo {
enum Level
{
HIGH,LOW,MEDIUM
}
public static void main(String args[])
{
Level l1;
Scanner sc=new Scanner(System.in);
System.out.print("enter the level:");
String level=sc.nextLine();
l1=Level.valueOf(level.toUpperCase());

switch(l1)
{
case HIGH: System.out.println("level is high");
break;
case LOW: System.out.println("level is low");
break;
case MEDIUM: System.out.println("level is medium");
break;
}
}
}


Example 2:
Enum in java is a data type that contains fixed set of constants.
It can be used for days of the week (SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY and SATURDAY) , directions (NORTH, SOUTH, EAST and WEST)
Months
etc.


Program :

import java.util.*;
import java.text.*;
public class MyClass
{
enum Days
{
SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY, SATURDAY
}

public static void main(String args[])
{
  
Date now = new Date();
System.out.println(now);
SimpleDateFormat d= new SimpleDateFormat("EEEEE");
String day=d.format(now);

Days da=Days.valueOf(day.toUpperCase());
     switch(da)
      {
          case MONDAY:
              System.out.println("Today is monday");
              break;
             
           case TUESDAY:
               System.out.println("Today is Tuesday");
              break;
             
           case WEDNESDAY:
               System.out.println("Today is wednesday");
             
               break;
           case THURSDAY:
               System.out.println("Today is Thursday");
              break;
           case FRIDAY:
               System.out.println("Today is Friday");
              break;
            case SATURDAY:
              System.out.println("Today  is saturday");
              break;       
              case SUNDAY:System.out.println("Today is sunday");
              break;
      }
     
    }
}


 Output:
Wed Jun 23 20:24:48 GMT 2021
Today is wednesday


Example 3:


enum Person
{
Married, Unmarried, Divorced, Widowed
}
The identifiers like Married, Unmarried etc. are called as enumeration Constants. Each such constant is implicitly considered as a public static final member of Person.
After defining enumeration, we can create a variable of that type. Though enumeration is a class type,
we need not use new keyword for variable creation, rather we can declare it just like any primitive data
type.
For example,

Person p= Person.Married;
We can use == operator for comparing two enumeration variables.
They can be used in switch-case
also. Printing an enumeration variable will print the constant name.
That is,System.out.println(p); // prints as Married.

Write a java program to illustrate working of enumerations.


Program :
public class EnumDemo
{
enum Person
{
Married, Unmarried, Divorced, Widowed
}
public static void main(String args[])
{
Person p1;

p1=Person.Unmarried;
System.out.println("Value of p1 :" + p1);
Person p2= Person.Widowed;
if(p1==p2)
System.out.println("p1 and p2 are same");
else
System.out.println("p1 and p2 are different");
switch(p1)
{
case Married: System.out.println("p1 is Married");
break;
case Unmarried: System.out.println("p1 is Unmarried");
break;
case Divorced: System.out.println("p1 is Divorced");
break;
case Widowed: System.out.println("p1 is Widowed");
break;
}
}
}

Output:

Value of p1 :Unmarried
p1 and p2 are different
p1 is Unmarried


Can Enum constants can be declared as static and final?
Yes. Enum constants are public static and final and are accessible directly using enum name.


Can a constuctor be invoked using an Enum?
 Yes we can use the constuctor with the name of Enum.
enum Level
{ HIGH,LOW,MEDIUM}
Level
{
System.out.print("This is constructor ");
}

Comments

Popular posts from this blog

Control Statements:Selection statement ,Iteration statement and Jump statement in Java

Applets - Inheritance hierarchy for applets, differences between applets and applications, life cycle of an applet, passing parameters to applets, applet security issues.

Java Database Connectivity (JDBC) ,Steps to connect to the database in Java