Suresh Rohan's Blog

This blog is all about Java, J2EE,Spring, Angular, React JS, NoSQL, Microservices, DevOps, BigData, Tutorials, Tips, Best practice, Interview questions, Views, News, Articles, Techniques, Code Samples, Reference Application and much more

Thursday, October 22, 2015

Enum Data Types


Where you want to restrict the user to providing input from a predefined list.


you might want the user to choose from a set of constants defining several printer types:

public static final int DOTMATRIX = 1;
public static final int INKJET = 2;
public static final int LASER = 3;

The solution works. In this case, however, you could pass any other integer (say 10), and compiler would happily take it. Therefore, this solution is not a typesafe solution(Type safety means that the compiler will validate types while compiling, and throw an error if you try to assign the wrong type to a variable.).


Enumerations are sets of closely related items, for example:
  • Directions - north, south, east, west
  • Types of novels - mystery, classic, fantasy, romance, science-fiction
  • Flavours of ice cream - chocolate, vanilla, raspberry, maple

Type-safe enumerations were added in JDK 1.5, and represent a special kind of class. If JDK 1.5 is not available, type-safe enumerations can still be implemented as a regular Java class.

Alternative to the simple String or int constants used to represent sets of related items.


Points to Remember
  • enums are implicitly final subclasses of java.lang.Enum
  • if an enum is a member of a class, it's implicitly static
  • new can never be used with an enum, even within the enum type itself
  • name and valueOf simply use the text of the enum constants, while toString may be overridden to provide any content, if desired
  • for enum constants, equals and == amount to the same thing, and can be used interchangeably
  • enum constants are implicitly public static final
  • the order of appearance of enum constants is called their "natural order", and defines the order used by other items as well : compareTo, iteration order of values , EnumSet, EnumSet.range.
  • enums have a built-in serialization mechanism, which can't be overridden. The mechanism uses the name and valueOf methods.
Example 1 - Basic

// define an enum for classifying printer types

enum PrinterType {

DOTMATRIX, INKJET, LASER

}



// test the enum now
public class EnumTest {
PrinterType printerType;
public EnumTest(PrinterType pType) {
printerType = pType;
}
public void feature() {
// switch based on the printer type passed in the constructor
switch(printerType){
case DOTMATRIX:
System.out.println("Dot-matrix printers are economical and almost obsolete");
break;
case INKJET:
System.out.println("Inkjet printers provide decent quality prints");
break;
case LASER:
System.out.println("Laser printers provide best quality prints");
break;
}
}
public static void main(String[] args) {
EnumTest enumTest = new EnumTest(PrinterType.LASER);
enumTest.feature();
}


}

Example 2 - member attributes and methods in an enum data type

PrinterType.java

public enum PrinterType {
DOTMATRIX(5), INKJET(10), LASER(50);
private int pagePrintCapacity;
private PrinterType(int pagePrintCapacity) {
this.pagePrintCapacity = pagePrintCapacity;
}
public int getPrintPageCapacity() {
return pagePrintCapacity;
}


}

// EnumTest.java

public class EnumTest {
PrinterType printerType;
public EnumTest(PrinterType pType) {
printerType = pType;
}
public void feature() {
switch (printerType) {
// omitted - refer previous example

case LASER:
System.out.println("Laser printers provide the best quality prints");
break;
}
System.out.println("Print page capacity per minute: " +
printerType.getPrintPageCapacity());
}
public static void main(String[] args) {
EnumTest enumTest1 = new EnumTest(PrinterType.LASER);
enumTest1.feature();
EnumTest enumTest2 = new EnumTest(PrinterType.INKJET);
enumTest2.feature();
}


}
However, the enum class cannot have a public constructor, or the compiler will complain with following message: "Illegal modifier for the enum constructor; only private is permitted."


 A constructor in an enum class can only be specified as private.

Best Practices 


public enum PrinterType {
       DOTMATRIX(5), INKJET(10), LASER(50);
       private int pagePrintCapacity;
       private PrinterType(int pagePrintCapacity) {
this.pagePrintCapacity = pagePrintCapacity;
}
       public int getPrintPageCapacity() {
return pagePrintCapacity;
}
       public static pagePrintCapacity getPrinterType(int pagePrintCapacity)      
             for(PrinterType printerType: PrinterType.values()){
if(pagePrintCapacity == printerType.getValue() )
return pagePrintCapacity;
}
return null;
}
}

No comments:

Post a Comment