A Developer Gateway To IT World...

Techie Uncle Software Testing Core Java Java Spring C Programming Operating System HTML 5 Java 8 ES6 Project

Java Enums:

Java Enums

What are Java Enums?

This is also called as java enumerated types.
Enums were introduced in java 5.0.
Enums restrict a variable to have one of only a few predefined values.
The values in this enumerated list are called enums.

With the use of enums, it is possible to reduce the number of bugs in your code.

For example,
If we consider an application for a fresh juice shop. It would be possible to restrict the glass size to small, medium and Large. This would make sure that it would not allow anyone to order any size other than the small, medium or large.

Example of Enums Types in Java:-






class FreshJuice 
{
 enum FreshJuiceSize
 { 
  SMALL, MEDUIM, LARGE 
 } 
 FreshJuiceSize size;
}

public class FreshJuiceTest 
{
 public static void main(String args[])
 { 
  FreshJuice juice = new FreshJuice();
  juice.size = FreshJuice. FreshJuiceSize.MEDUIM ;
 }
}


 



Note: enums can be declared as their own or inside a class.
Methods, variables, constructors can be defined inside enums as well.

LEARN TUTORIALS

.

.