Java Enum

“With great power comes great responsibility”.

So we will start out discussion with, what are classes in java? Class is a set of properties or methods that are common to objects of that particular class. In java the class is declared by class keyword as same in C++. While declaring the class first we have to use class keyword, then class name we want to give. Also mention the modifiers for the class i.e. public, final, abstract, etc. Then we can add the methods and data members of the class, this is nothing but the body of the class.

Access Modifiers

So Before going to enums let us discuss about access modifiers in java. So modifiers (Access Modifiers) are basically keywords in java which are used to specify the accessibility or scope of a field, method, constructor or a class. Here let us talk about 3 access modifiers in java which we will be needing to understand enums.

Enum in Java

In java, enum is represented as a special type of class that contains the set of constants (unchangeable variables like final) and it was first introduced in java version 1.5. It is used where all the values are known at compile time (before executing the program). As we declare class using the class keyword and as enum is special type of class, enums are declared using enum keyword instead of class keyword. We can declare a enum inside a class or outside a class but not inside a method. Also if we declare the enum inside the class then we can also apply modifiers like public, private, etc. While declaring enum, first line contains constants (Constants are objects of that enum itself.) and then other things like constructor, methods, variables, etc. All the constants must be written in upper case and also by default they are public, static, final. We have already discussed about public, static, final (access modifiers) at start of the blog.

Methods in Java Enum

So far we have seen the basics of enum i.e. how it is created, how to declare the enum, the constructor, etc. Now we will see the methods of enum. All enums implicitly extends the java.lang.Enum class so the methods written in the java.lang.Enum are available in enum like toString(), valueOf(), etc. Also in java multiple inheritance is not possible so enum cannot extends other classes as it already extends from java.lang.Enum class. Now we will see each method used with enums.

Enum implementing Interface

So as we discussed previously that enum cannot extend from other classes as it already extends from java.lang.Extend. However, it is interesting to note that though enums cannot extend from other classes they can implement interfaces. Following is the example of an enum implementing an interface -

Enums with switch statement

Enums can also used with switch statement. We will understand it by following example.

Enum in java vs. Enum in C++

Applications of enum

  1. Rahul Kanade
  2. Shubham Kasar
  3. Shruti Kokate
  4. Adarsh Londhe
  5. Kshitij Magare



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store