Tuesday, 2 August 2016

Java Enums

1)  Enums were introduced in java 5.0. Enums restrict a variable to have one of only a few predefined values.
2)  The values in this enumerated list are called enums.
3)  With the use of enums, it is possible to reduce the number of bugs in your code.
4)  For example, if we consider an application for a fresh juice shop, it would be possible to restrict the glass size to
5)  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

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 ;

System.out.println("juice size"+" "+juice.size);