Saturday, May 30, 2015

enum


// 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.

public class EnumDemo {

enum FruitJuseSize{SMALLL, MEDIUM, LARGE, XLARGE}
FruitJuseSize size;

public static void main(String[] args) {

EnumDemo obj = new EnumDemo();
obj.size = FruitJuseSize.SMALLL;
System.out.println(obj.size);
}
}


--------------------------------------------Another Example ----------------------------------------------



public class AnotherWay {

public static void main(String[] args) 
{
FruitJuiceList obj = new FruitJuiceList();
obj.size = FruitJuiceList.FruitJuice.LARGE;
System.out.println(obj.size);
}
}

// ------Class FruitJuiceList------


class FruitJuiceList
{
enum FruitJuice{SMALL, MEDIUM, LARGE}
FruitJuice size;
}


No comments:

Post a Comment