Can we have variables and methods in an enum in Java?


Enumeration (enum) in Java is a datatype which stores a set of constant values. You can use enumerations to store fixed values such as days in a week, months in a year etc.

You can define an enumeration using the keyword enum followed by the name of the enumeration as −

enum Days {
   SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
}

Methods and variables in an enumeration

Enumerations are similar to classes and, you can have variables, methods, and constructors within them. Only concrete methods are allowed in an enumeration.

Example

In the following example, we are defining an enumeration with name Vehicles and declaring five constants in it.

In addition to it we are having a constructor, instance variable and an instance method.

 Live Demo

enum Vehicles {
   //Declaring the constants of the enum
   ACTIVA125, ACTIVA5G, ACCESS125, VESPA, TVSJUPITER;
   //Instance variable of the enum
   int i;
   //Constructor of the enum
   Vehicles() {}
   //method of the enum
   public void enumMethod() {
      System.out.println("This is a method of enumeration");
   }
}
public class EnumExample{
   public static void main(String args[]) {
      Vehicles vehicles[] = Vehicles.values();
      for(Vehicles veh: vehicles) {
         System.out.println(veh);
      }
      System.out.println("Value of the variable: "+vehicles[0].i);
      vehicles[0].enumMethod();
   }
}

Output

ACTIVA125
ACTIVA5G
ACCESS125
VESPA
TVSJUPITER
Value of the variable: 0
This is a method of enumeration

Example

In the Following Java example, we are declaring 5 constants with values, we have an instance variable to hold the value(s) of the constant, a parameterized constructor to initialize it and a getter method to get these values.

 Live Demo

import java.util.Scanner;
enum Scoters {
   //Constants with values
   ACTIVA125(80000), ACTIVA5G(70000), ACCESS125(75000), VESPA(90000), TVSJUPITER(75000);
   //Instance variable
   private int price;
   //Constructor to initialize the instance variable
   Scoters (int price) {
      this.price = price;
   }
   //Static method to display the price
   public static void displayPrice(int model){
      Scoters constants[] = Scoters.values();
      System.out.println("Price of: "+constants[model]+" is "+constants[model].price);
   }
}
public class EnumerationExample {
   public static void main(String args[]) {
      Scoters constants[] = Scoters.values();
      System.out.println("Value of constants: ");
      for(Scoters d: constants) {
         System.out.println(d.ordinal()+": "+d);
      }
      System.out.println("Select one model: ");
      Scanner sc = new Scanner(System.in);
      int model = sc.nextInt();
      //Calling the static method of the enum
      Scoters.displayPrice(model);
   }
}

Output

Value of constants:
0: ACTIVA125
1: ACTIVA5G
2: ACCESS125
3: VESPA
4: TVSJUPITER
Select one model:
2
Price of: ACCESS125 is 75000

Updated on: 30-Jul-2019

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements