X

Play AlgoQuiz

Rate Us :



Share with Friends :

Menu

Enumeration :

We can use enum to define a group of named constants.

By using enum we can define our own data types. enum concept was introduced in version 1.5 of Java. When compared with old languages like C++, enum in Java is more powerful.

enum Country
{
 India, USA, China....; // terminator (;) is optional
}

Internal implementation of enum :

enum concept is internally implemented by using class concept. Every enum constant is a reference variable to enum type object. Every enum constant is always public static final by default.

We can declare enum either within the class or outside the class but not inside a method.

Example :

enum Country { India, USA, China; } class Example { public static void main(String [ ] args) { Country c = Country.India; System.out.println(c); } }

Output :

India

If we are trying to declare enum in the method then we will get compile time error.

enum A
{

}
class B
{

} // valid
class B
{
 enum A
 {

 } 
} // valid
class B
{
 public void m1()
 {
  enum A
  {

  }
 }  
} // Compile time error : enum types must not be local

If we are declaring enum outside the class then the applicable modifiers are public, <default>, strictfp. If we declare enum within the class then the applicable modifiers are public, <default>, strictfp, private, protected, static.


Important points :

Until version 1.4 of Java, the allowed data types for switch argument are byte, short, char, int. But from Java 1.5 version onwards in addition to the listed data types their corresponding wrapper classes Byte, Short, Character, Integer and enum type also allowed. Hence, from 1.5 version onwards we can use enum as argument to switch statement.

Example :

enum Country { India, USA, China; } class Example { public static void main(String [ ] args) { Country c = Country.India; switch(c) { case USA : System.out.println("USA"); break; case India : System.out.println("India"); break; case China : System.out.println("China"); break; default : System.out.println("Please choose a country!"); } } }

Output :

India

If we are passing enum type as argument to switch statement then every case level should be a valid enum constant.

Every enum in Java is direct child class of java.lang.Enum

As every enum is always extending java.lang.Enum hence, there is no chance of extending any other enum (because Java won't provide support for multiple inheritance).

As every enum is always final implicitly hence, we can't create child enum for our enum. This is the reason due to which inheritance is not applicable for enum explicitly. But enum can implement any number of interface at a time.

enum E
{

}
enum F extends E
{

} // invalid
enum E extends java.lang.Enum
{

} // invalid
enum E
{

}
class F extends E
{

} // invalid
class E
{

}
enum F extends E
{

} // invalid
interface E
{

}
enum F implements E
{

} // valid

java.lang.Enum :

Every enum in Java is always direct child class of java.lang.Enum class. The power of enum is inheriting from this class to our class. It is an abstract class and direct child class of Object class. This class implements Comparable and Serializable interface. Hence, every enum in Java is by default Serializable and Comparable.


values() method :

We can use values() method to list out all the values of enum.

Example :

enum Country { India, USA, China; } class Example { public static void main(String [ ] args) { Country[ ] c = Country.values(); for(Country c1 : c) System.out.println(c1); } }

Output :

India USA China

ordinal() method :

Within the enum, the order of constant is important which can be specify by using ordinal value. We can find ordinal value of enum constant by using ordinal() method.

Example :

enum Country { India, USA, China; } class Example { public static void main(String [ ] args) { Country[ ] c = Country.values(); for(Country c1 : c) System.out.println(c1 + " " + c1.ordinal()); } }

Output :

India 0 USA 1 China 2

Speciality in enum of Java :

When compared with old languages enum, Java enum is more powerful because in addition to constants we can take variables, methods, constructors etc. which may not be possible in old languages. This extra features is due to internal implementation of enum concept which is child based. Inside enum we can declare main() method and hence we can invoke class which has enum, directly from command prompt.

Example :

enum Example { India, USA, China; // terminator (;) is compulsory public static void main(String [ ] args) { System.out.println("main() method of enum"); } }

Output :

save as : Example.java compile as : javac Example.java run as : java Example main() method of enum

In addition to constant if we want to take any extra members then it is compulsory that the list of the constants should be in the first line and should ends with terminator (;).

enum Shape
{
 Circle, Square, Rectangle, Triangle
 public void m1()
 {
 
 }
} // invalid
enum Shape
{
 public void m1()
 {
 
 }
 Circle, Square, Rectangle, Triangle;
} // invalid
enum Shape
{
 Circle, Square, Rectangle, Triangle;
 public void m1()
 {
 
 }
} // valid

Inside enum without having constant we can't take any extra members but empty enum is always valid.

enum Shape
{
 public void m1()
 {
 
 }
} // invalid
enum Shape
{

} // valid

Enum class constructor :

Within Enum class we can take constructor also. Enum class constructor will be executed automatically at the time of Enum class loading because enum constants will be created at the time of class loading. We can't invoke Enum class constructor explicitly.

Example :

enum Country { India, USA, China; } class Example { public static void main(String [ ] args) { Country[ ] c = Country.values(); for(Country c1 : c) System.out.println(c1); } }

Output :

Enum class constructor Enum class constructor Enum class constructor India USA China

We can't create object of Enum explicitly and hence, we can't call constructor directly.

Within the Enum we can take instance and static methods but we can't take abstract methods. Every enum constant represents an object hence, whatever method which we can apply on normal Java object can be apply in enum constants also.


Conclusion :


enum :

It is a keyboard which can be used to define a group of named constants.


Enum :

It is a class present in java.lang package which acts as a base class for all Java enum.


Enumeration :

It is an interface present in java.util package which can be used for retrieving objects from collection one by one.



X

MiniDoll :

MiniDoll is an application to learn with fun. You can play it to solve some tricky questions related to Java Programming. Either enjoy playing it alone or discuss with your friends to solve it. It is a simple approach to learn by solving questions. This application has different topics of Java and each topic is divided into three phases. Start solving from initial topic to final topic and by the end of every topic you can boost your concept of that topic.