Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Monday 29 May 2017

Java Enum

 Enum In Java

Java Enum

Enum in java is a special type of data type. Enum is a collection of constants and these constants are by default static and final in java. 

Enum java can be used for a days of week(SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY), can be used for direction(EAST, WEST, NORTH, SOUTH) and can be used for month.

Java enum are available from JDK 1.5.

Java enum is an abstract class in java API, Which is implements Serializable  and Cloneable interfaces in java but it cannot extend any class because it internally extends Enum class.


How to define Enum in Java

public enum Direction
{
EAST,
WEST,
NORTH,
SOUTH
}

In the above declaration, Direction is the variable of enum type and it contains four constants EAST, WEST, NORTH, SOUTH.

Note : Java enum can be used within the class and outside of a class. You will understand better in given below java enum examples.


How to assign value to a enum type ?

Let's see how to assign value to enum type in java.

Direction d = Direction.SOUTH;

Here d is a variable of type Direction and Direction is a type of enum. This variable can take any value from the list(EAST, WEST, NORTH AND SOUTH). But in this case the value is set SOUTH.


Java Enum Example

The java enum can be defined within the class and outside of a class. In this example we are going to use java enum outside of the class.

enum Directions
{
EAST,
WEST,
NORTH,
SOUTH
}
class EnumExample1
{
public static void main(String args[])
{
Directions d = Directions.WEST;
System.out.println(d);
}
}

output : WEST


Java Enum Example 2



This is another java enum example, Where we will use java enum within the class.

public class EnumExample2
{
enum Days
{
SUNDAY,
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY,
}
public static void main(String args[])
{
Days dd = Days.WEDNESDAY;
System.out.println(dd);
}


output : WEDNESDAY


Java Enum Example 3

In this example we are going to use values() method which returns an array of all enum values.

enum Directions
{
EAST,
WEST,
NORTH,
SOUTH
}
class EnumExample3
{
public static void main(String args[])
{
for(
Directions d : Directions.values());
{
System.out.println(d);
}
}
}

output : EAST
              WEST
              NORTH
              SOUTH



Java Enum Example With if-else Statements

We can use enum with if-else conditions in java programming.

enum Directions
{
EAST,
WEST,
NORTH,
SOUTH
}
class EnumWithIfElse
{
public static void main(String args[])
{
Directions d = Directions.SOUTH;
if(d == Directions.EAST)
{
System.out.println("EAST Direction");
}
else if(d == Directions.WEST)
{
System.out.println("WEST Direction");
}
else if(d == Directions.NORTH)
{
System.out.println("NORTH Direction");
}
else
{
System.out.println("SOUTH Direction");
}
}
}

output : SOUTH Direction

Java Enum Example With switch Statements

In this example we are going to use switch-case statements with java enum.

enum Directions
{
EAST,
WEST,
NORTH,
SOUTH;//here semicolon is optional 
}
class EnumWithSwitchCase
{
public static void main(String args[])
{
Directions d = Directions.NORTH;

//using switch-case statements
switch(d)
{
case EAST :
System.out.println("this is EAST");
break;
case WEST :
System.out.println("this is WEST");
break;
case NORTH :
System.out.println("this is NORTH");
break;
case SOUTH :
System.out.println("this is SOUTH");
break;
default:
System.out.println("Day Not Matched");
}
}
}

output : this is NORTH


Java Enum Example with Initialization, Field, Constructor and Method 

We can use field, constructor and methods in java enum program and we can give specific values to all the constants of java enum. The java enum constants have initial values start from 0,1,2,3 and so on.

enum Directions
{
EAST("E"),
WEST("W"),
NORTH("N"),
SOUTH("S");//here semicolon is required otherwise give c. error

//declaration of field
private final String field;

//declaration of constructor
Directions(String s)
{
field = s;
}

public String directionInformation()
{
return field;
}
}
class EnumAll
{
public static void main(String args[])
{
Directions d = Directions.EAST;
System.out.println(d.directionInformation());
Directions d1 = Directions.WEST;
System.out.println(d1.directionInformation());
}
}

output : E
Share:

0 comments:

Post a Comment

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate