Friday, January 30, 2009

JAVA ENUM Key Notes for SCJP

Enumerations are set of closely related items, often called Type Safe Enum that are introduced in JDK 5. It is the better way to define constants, but it is more than that. you can read and test the examples from online sun tutorials on enums.

Couple of Key notes that I have noted are as followings:

  1. enums are implicitly final subclasses of java.lang.Enum that is inherited from java.lang.Object.
  2. As enums are implicitly final classes, so inheritance between enums or simple classes is not possible.
  3. If an enum is member of a class it is implicitly static.
  4. new keyword can never be used with enum.
  5. name() and valueOf() methods simply use the text of enum constant.
  6. Defualt implementation of toString() returns the name of the enum constant, but you can override its implementation.
  7. For enum constants equals() and == amount to the same thing and can be used interchangebly.
  8. enum constants are implicitly public, static and final
  9. The order of appearance of enum constants is called their NATURAL ORDER
  10. There is a built in method named as values() that returns the array of all enum constants.
  11. All instances of Enums are serializable by default. As Enums are subclasses of class java.lang.Enum, which implements the interface java.io.Serializable. If a class implements the interface java.io.Serialisable, its objects are serializable.
  12. Enums are compiled to a .class file.
  13. An enum can define a main method and it can be executed as a standalone application
  14. You can provide the abstract methods in the enum, but this abstract method needs to be implemented by all of the enum constants because we can not declare enum itself as abstract. Simply We cannot define abstract enums.
  15. Enums cannot be instantiated, we can only create the reference of an enum
  16. Enums may be used as a operand for the instanceof operator in the same way that it can be used by a class
  17. Enums can be used in switch statements.
  18. An enum can define more than 1 constructor (similar to a class) and its constructor can accept more than 1 method parameter. Please note we can not create the actual object using new like we do with classes.
  19. name() is a final method, which cannot be overridden. It returns the name of the enum constant, exactly as declared in its enum declaration.
  20. Constructors for an enum type should be declared as private. The compiler allows non
    private declares for constructors, but this seems misleading to the reader, since new can
    never be used with enum types.
  21. Constructor of enum can only be private or default. public and protected are not allowed with constructor of Enum.
  22. Enum can implement interface.
  23. Enums cannot be declared within a method!

1 comment:

mallesh said...

Thanks for the share ..Really appreciated work. And I do have the SCJP Dumps | SCWCD Dumps | SCJP Tutorials...I think this is helpful to ur visitors