A enum Type Declaration, Which Uses Fields, Constructors, and Methods - Java Language Basics

Java examples for Language Basics:enum

Description

A enum Type Declaration, Which Uses Fields, Constructors, and Methods

Demo Code

public class Main {
  public static void main(String[] args) {
    for(Level s : Level.values()) {
      String name = s.name();//w ww  .j  a v  a 2s  . c o  m
      int ordinal = s.ordinal();
      int days = s.getValue();  
      System.out.println("name=" + name + 
          ", ordinal=" + ordinal + ", days=" + days);
    }
  }
}
enum Level {
    LOW(30), MEDIUM(15), HIGH(7), URGENT(1);
    
    // Declare an instance variable  
    private int value;
    
    // Declare a private constructor  
    private Level(int a) {
        this.value = a;
    }
    
    // Declare a public method to get the turnaround days  
    public int getValue() {
        return value;
    }
}

Result


Related Tutorials