Java 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.
For example:
class FreshJuice {
enum FreshJuiceSize{ SMALL, MEDIUM, LARGE }
enum FreshJuiceSize{ SMALL, MEDIUM, LARGE }
FreshJuiceSize size;
}
public class FreshJuiceTest {
public static void main(String args[]){
FreshJuice juice = new FreshJuice();
juice.size = FreshJuice. FreshJuiceSize.MEDIUM ;
System.out.println("Size: " + juice.size);
}
}
Result will be:
Size: MEDIUMNote: enums can be declared as their own or inside a class. Methods, variables, constructors can be defined inside enums as well.
Comments
Post a Comment