DekGenius.com
JAVA
Java Enum
enum Size {
SMALL, MEDIUM, LARGE, EXTRALARGE
}
class Main {
public static void main(String[] args) {
System.out.println(Size.SMALL);
System.out.println(Size.MEDIUM);
}
}
Java Enums
public class Main {
enum Level {
LOW,
MEDIUM,
HIGH
}
public static void main(String[] args) {
Level myVar = Level.MEDIUM;
System.out.println(myVar);
}
}
java enum
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();
}
enum java
private enum VarState {
INIT,
TRUE,
FALSE,
UNSATISFIABLE
}
java enum
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
}
}
java enum
package vn.viettuts.javaenum;
public enum PageInfo {
// define enum with 3 variable
PAGE_001("PAGE_001", "Login", "/login"),
PAGE_002("PAGE_002", "Add new user", "/addUser"),
PAGE_003("PAGE_003", "List user", "/listUser");
private String pageID;
private String pageTitle;
private String pageURL;
/**
* constructor
*
* @param pageID
* @param pageTitle
* @param pageURL
*/
private PageInfo(String pageID, String pageTitle, String pageURL) {
this.pageID = pageID;
this.pageTitle = pageTitle;
this.pageURL = pageURL;
}
// getter & setter
public String getPageID() {
return pageID;
}
public void setPageID(String pageID) {
this.pageID = pageID;
}
public String getPageTitle() {
return pageTitle;
}
public void setPageTitle(String pageTitle) {
this.pageTitle = pageTitle;
}
public String getPageURL() {
return pageURL;
}
public void setPageURL(String pageURL) {
this.pageURL = pageURL;
}
}
java enum
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");
}
}
}
java enum
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);
}
}
}
java enum
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);
}
}
Java enums
enum Size {
SMALL, MEDIUM, LARGE, EXTRALARGE
}
Enum Class in Java
enum Size {
constant1, constant2, …, constantN;
// methods and fields
}
Java Enum name()
name(SMALL)
// returns "SMALL"
© 2022 Copyright:
DekGenius.com