JAVA ENUM:Key Notes for SCJP

by shaiekh on November 21, 2009

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.

Some of Key notes about enum:

  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 static 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. enums can be declared as their own class, or enclosed in another class.
  14. An enum can define a main method and it can be executed as a standalone application.
  15. 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.
  16. Enums cannot be instantiated, we can only create the reference of an enum.
  17. Enums may be used as a operand for the instanceof operator in the same way that it can be used by a class.
  18. Enums can be used in switch statements.
  19. You can NEVER invoke an enum constructor directly. The enum constructor is invoked automatically, with the arguments you define after the constant value.
  20. An enum can define more than one constructor (similar to a class) and its constructor can accept more than one method parameter. Please note we can not create the actual object using new like we do with classes.
  21. name() is a final method, which cannot be overridden. It returns the name of the enum constant, exactly as declared in its enum declaration.
  22. 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.
  23. Constructor of enum can only be private or default. public and protected are not allowed with constructor of Enum.
  24. Enum can implement interface.
  25. Enums cannot be declared within a method!

Download some practice questions for better understanding of java enum.


Bookmark and Share

Leave a Comment

Previous post: Microsoft Office 2007 Add-in: Save as PDF

Next post: Make Your Portable Applications using WinRAR