We use enum types any time we need to represent a fixed set of constants in our programs. An enumeration is a new type since Java 5(jdk1.5) . Prior to jdk 1.5 typesafe Enumeration pattern was a ususal way to implement a type that can only take a finite number of values. My aim is not to go into the enumeration pattern, but below is an example of how this enumeration pattern may be used instead of Enums.
There are several benefits that we get when using the enum keyword, and hence I do not prefer using the enumeration pattern.
Declaring an enum in java:
public enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES }
It is not required that you write enum constants in caps, but this is a good programming practice to do so.
We can define an Enum as a separate class or inside a class, but always remember that we cannot define an enum inside a method.
Defining as a separate class:
Declaring an enum in java:
public enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES }
It is not required that you write enum constants in caps, but this is a good programming practice to do so.
We can define an Enum as a separate class or inside a class, but always remember that we cannot define an enum inside a method.
Defining as a separate class:
Defining enum inside the class
Notice the syntax of accessing the enum’s members depends on where the enum was declared.
It is optional to put a semicolon at the end of the enum declaration , do not get confused !!!
Now when I defined an enum what did actually got created ? I mean what is this HEARTS or SPADES or any other enum constant that you declare. Is it a String? Or Is it an int? NO!!!
Always remember – They are instances. Each enum constant corresponds to a call to a constructor.
The key point to remember about the enum constructor is that programmers cannot invoke this constructor. It is for use by code emitted by the compiler in response to enum type declarations. Hence new keyword can never be used with an enum , even not within the enum type itself.
All enums implicitly extend java.lang.Enum. Since Java does not support multiple inheritance, an enum cannot extend anything else.
Always remember – They are instances. Each enum constant corresponds to a call to a constructor.
The key point to remember about the enum constructor is that programmers cannot invoke this constructor. It is for use by code emitted by the compiler in response to enum type declarations. Hence new keyword can never be used with an enum , even not within the enum type itself.
All enums implicitly extend java.lang.Enum. Since Java does not support multiple inheritance, an enum cannot extend anything else.
ava programming language enum types are much more powerful than their counterparts in other languages because there are some special methods which the compiler automatically adds when it creates an enum. For example, they have a static values() method that returns an array containing all of the values of the enum in the order they are declared.
values() method is generated by the compiler for each individual enum class. It's static, and it's not an inherited method.
Some facts about enums in java:
1.)If enum is a member of a class it is implicitly static.
2.)enum constants are implicitly public static final.
3.)equals() and == return same thing for enum.
4.)Programmers cannot invoke the enum constructor, in other word new keyword can never be used with an enum .
5.)each enum constant corresponds to a call to constructor.
6.) enums can be used in switch()
7.)One enum cannot extend another enum or Class.
8.) enums cannot be defined inside any method.
1.)If enum is a member of a class it is implicitly static.
2.)enum constants are implicitly public static final.
3.)equals() and == return same thing for enum.
4.)Programmers cannot invoke the enum constructor, in other word new keyword can never be used with an enum .
5.)each enum constant corresponds to a call to constructor.
6.) enums can be used in switch()
7.)One enum cannot extend another enum or Class.
8.) enums cannot be defined inside any method.