Cyberspark
0
Q:

what is enumeration in java


public enum Day {
    SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
    THURSDAY, FRIDAY, SATURDAY 
}
5
// A simple enum example where enum is declared 
// outside any class (Note enum keyword instead of 
// class keyword) 
enum Color 
{ 
    RED, GREEN, BLUE; 
} 
  
public class Test 
{ 
    // Driver method 
    public static void main(String[] args) 
    { 
        Color c1 = Color.RED; 
        System.out.println(c1); 
    } 
}
4
public class DaysOfTheWeek {

    public enum Days {m, t, w, r, f, sat, s};
  	    public static void main(String[] args) {
          Days d = Days.t; 
          System.out.println(d);
          //the output would be t
        }
}
4
// Java program to demonstrate that enums can have constructor 
// and concrete methods. 
  
// An enum (Note enum keyword inplace of class keyword) 
enum Color 
{ 
    RED, GREEN, BLUE; 
  
    // enum constructor called separately for each 
    // constant 
    private Color() 
    { 
        System.out.println("Constructor called for : " + 
        this.toString()); 
    } 
  
    public void colorInfo() 
    { 
        System.out.println("Universal Color"); 
    } 
} 
  
public class Test 
{     
    // Driver method 
    public static void main(String[] args) 
    { 
        Color c1 = Color.RED; 
        System.out.println(c1); 
        c1.colorInfo(); 
    } 
} 
0

New to Communities?

Join the community