xxxxxxxxxx
enum Size {
SMALL, MEDIUM, LARGE, EXTRALARGE
}
class Main {
public static void main(String[] args) {
System.out.println(Size.SMALL);
System.out.println(Size.MEDIUM);
}
}
xxxxxxxxxx
public class Main {
enum Level {
LOW,
MEDIUM,
HIGH
}
public static void main(String[] args) {
Level myVar = Level.MEDIUM;
System.out.println(myVar);
}
}
xxxxxxxxxx
```
// Direction.java
public enum Direction {
EAST("East", 42),
WEST("West", 30),
NORTH("North", 25),
SOUTH("South", 20);
private final String name;
private final int windSpeed;
private Direction(String name, int windSpeed) {
this.name = name;
this.windSpeed = windSpeed;
}
public String getName() {
return name;
}
public int getWindSpeed() {
return windSpeed;
}
// Method to create a new instance with changed wind speed (NOT RECOMMENDED)
public Direction changeWindSpeed(int newWindSpeed) {
return new Direction(name, newWindSpeed);
}
}
// Main.java
public class Main {
enum Color { RED, GREEN, BLUE }
public static void main(String[] args) {
// Using the enum
Direction currentDirection = Direction.EAST;
// Accessing methods
System.out.println("Current direction is " + currentDirection.getName());
System.out.println("Wind speed is " + currentDirection.getWindSpeed() + " mph");
// Using the method to change wind speed
Direction newDirection = currentDirection.changeWindSpeed(50);
System.out.println("New direction is " + newDirection.getName());
System.out.println("New wind speed is " + newDirection.getWindSpeed() + " mph");
// Get the third value (index 2) in the Color enum
Color thirdColor = Color.values()[2];
System.out.println("Third color: " + thirdColor);
}
}
```
xxxxxxxxxx
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
}
}
xxxxxxxxxx
package vn.viettuts.javaenum;
public class EnumExample5 {
// define enum Season
enum Season {
SPRING(5), SUMMER(10), FALL(15), WINTER(20);
private int value;
private Season(int value) {
this.value = value;
}
}
public static void main(String args[]) {
for (Season s : Season.values()) {
System.out.println(s + " " + s.value);
}
}
}
xxxxxxxxxx
package org.o7planning.tutorial.javaenum;
public enum Currency {
VND, USD, EURO ;
@Override
public String toString() {
if (this == VND) {
return "Dong";
} else if (this == USD) {
return "Dola";
}
return super.toString();
}
public static void main(String[] args) {
Currency money = Currency.USD;
System.out.println("Money " + money);
System.out.println("Money " + money.toString());
System.out.println("VND " + Currency.VND);
System.out.println("EURO " + Currency.EURO);
}
}
xxxxxxxxxx
package org.o7planning.tutorial.javaenum;
public enum Color {
RED("red") {
@Override
public String getHexCode() {
return "#ff0000";
}
},
GREEN("green") {
@Override
public String getHexCode() {
return "#00ff00";
}
},
BLUE("blue") {
@Override
public String getHexCode() {
return "#0000ff";
}
};
private String name;
Color(String name) {
this.name = name;
}
public String getName() {
return name;
}
public abstract String getHexCode();
}
xxxxxxxxxx
package vn.viettuts.javaenum;
public class EnumExample5 {
// define days of week
enum Day {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY,
SATURDAY, SUNDAY
}
public static void main(String args[]) {
Day day = Day.SUNDAY;
switch (day) {
case SUNDAY:
System.out.println("sunday");
break;
case MONDAY:
System.out.println("monday");
break;
default:
System.out.println("other day");
}
}
}