An
enum
is a special "class" that represents a group of constants (unchangeable variables, like final
variables).
To create an
enum
, use the enum
keyword (instead of class or interface), and separate the constants with a comma. Note that they should be in uppercase letters:Example
enum Level {
LOW,
MEDIUM,
HIGH
}
Enum: short form for enumeration: a group of named constant
- An enum is a special data type which is basically a collection (set) of constants
- Variables of that type must be equal to one of the constant values that have been predefined.
- An enum is a special class that defines a set of constants
- To create enum, use the enum keyword (instead of class or interface), separate the constants with comma.
- Constant should be uppercase letters.
- While defining enums, the constant should be declared first, prior to any filed or methods
- Semi Colon (;) is optional, however, when there are fields and methods declared inside enum, the list of enum constant must end with semicolon(;)
- enum concept introduced in 1.5 version
public enum Directions{
EAST, WEST, SOUTH, NORTH
}
Here, we have a variable Directions of enum type (class type variable), which is a collection of four constants EAST, WEST, SOUTH, and NORTH
Objective: If we want to represent a group of named constant then we should go for enum. The main objective of enum is to define our own data type(enumerated data type)
Internal implementation of enum:
- Every enum is internally implemented by using class concept
- Every enum constant is always public, static, and final
- Every enum constant represent an object of the type enum
- Every enum constant is static and hence we can access by using enum name.
- Inside enum, toString method is implemented to return name of the constant directly
package com.enumconcept;
public enum Currency {
PENNY(1), NICKLE(5), DIME(10), QUARTER(25);
private int value;
private Currency(int value) {
this.value = value;
}
public int getValue() {
return value;
}
}
package com.enumconcept;
public class EnumerationExample {
public static void main(String[] args) {
System.out.println("The value of dime is: " + Currency.DIME.getValue());
}
}
Output:
The value of dime is: 10
package com.cerotid.enumconcept;
public enum Weather {
//CHECKING, SAVING, BUSINESS_CHECKING;
//choose the account from there
//whenever you do the account setup
//Account.Accounttype = Accountype.business_checking
SUMMER ("summer"), WINTER ("winter"), FALL ("fall"), SPRING ("spring");
private String weather;
Weather(String weather){
this.weather = weather;
}
public String getWeather() {
return weather;
}
public void setWeather(String weather) {
this.weather = weather;
}
}
package com.cerotid.enumconcept;
public class WeatherTest {
public static void main(String[] args) {
//System.out.println(Weather.FALL);
Weather weather = Weather.FALL;
switch (weather) {
case SUMMER:
System.out.println("Go to vacation");
break;
case WINTER:
System.out.println("Go to Infront of fire place");
break;
case FALL:
System.out.println("Go to enjoy fall color");
break;
case SPRING:
System.out.println("Go to enjoy party");
break;
default:
break;
}
System.out.println(Weather.valueOf("SUMMER"));
}
}
output:
Go to enjoy fall color
No comments:
Post a Comment