Java Program to Create an Enum Class With Constructor and Method

You are currently viewing Java Program to Create an Enum Class With Constructor and Method

Java program to create an enum class with constructor and method

// Java program to create an enum class 
// with constructor and method

enum Vehicle {
  BIKE,
  CAR,
  BUS;

  private Vehicle() {
    System.out.println("Constructor called for : " + this.toString());
  }

  public void vehicleMethod() {
    System.out.println("Vehicle is running.");
  }
}

public class Main {
  public static void main(String[] args) {
    Vehicle veh = Vehicle.BUS;
    veh.vehicleMethod();
  }
}

Output

Constructor called for : BIKE
Constructor called for : CAR
Constructor called for : BUS
Vehicle is running.

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply