Detailed introduction to the use of enumerations in java
Enumeration features
1. Using enum to define an enumeration class inherits the java.lang.Enum class by default instead of inheriting the Object class. Among them, the java.lang.Enum class implements two interfaces, java.lang.Serializable and java.lang.Comparable
2. The constructor of the enumeration class can only use the private access modifier, if the access control of its constructor is omitted symbol, the private modification is used by default;
3. All instances of the enumeration class must be explicitly listed in the enumeration class, otherwise this enumeration class will never be able to generate instances. When these instances are listed, the system automatically adds public static final modifications without the need for programmers to add them explicitly.
public enum Week {
MON{
public String toLocaleString(){
return "星期一";
}
},TUES{
public String toLocaleString(){
return "星期二";
}
},WEB{
public String toLocaleString(){
return "星期三";
}
},THUR{
public String toLocaleString(){
return "星期四";
}
},FRI{
public String toLocaleString(){
return "星期五";
}
},SAT{
public String toLocaleString(){
return "星期六";
}
},SUN{
public String toLocaleString(){
return "星期日";
}
};
public abstract String toLocaleString();
}Traversal of enumerations
public class EnumTest {
public static void main(String[] args){
for(Week w:Week.values()){
System.out.println(w);
}
}
}Common methods of enumerations
int compareTo method
String name() returns the name of the enumeration instance
int ordinal() returns the enumeration value in the enumeration Index
String toString() returns the instance name of the enumeration which is more commonly used than name
public static valueOf()
public class EnumTest {
public static void main(String[] args){
Week day =Week.FRI;
System.out.println(day);//FRI
System.out.println(day.name());//FRI
System.out.println(day.ordinal());//4
System.out.println(Week.valueOf("SUN").toLocaleString());//星期日
System.out.println(Week.values().length);//7 获取枚举长度
}
}Constructor of the enumeration
public enum Gender {
MALE("男"),FEMALE("女");
private String name;
private Gender(String name){
this.name =name;
}
public String getName(){
return this.name;
}
public String toString(){
String name = null;
switch(this){
case MALE:
name="男";
break;
case FEMALE:
name="女";
break;
}
return name;
}
}Comprehensive application example of the enumeration: traffic light

public enum Lamp {
/*每个枚举元素各表示一个方向的控制灯*/
S2N("N2S","S2W",false),S2W("N2E","E2W",false),E2W("W2E","E2S",false),E2S("W2N","S2N",false),
/*下面元素表示与上面的元素的相反方向的灯,它们的“相反方向灯”和“下一个灯”应忽略不计!*/
N2S(null,null,false),N2E(null,null,false),W2E(null,null,false),W2N(null,null,false),
/*由南向东和由西向北等右拐弯的灯不受红绿灯的控制,所以,可以假想它们总是绿灯*/
S2E(null,null,true),E2N(null,null,true),N2W(null,null,true),W2S(null,null,true);
private Lamp(String opposite,String next,boolean lighted){
this.opposite = opposite;
this.next = next;
this.lighted = lighted;
}
/*当前灯是否为绿*/
private boolean lighted;
/*与当前灯同时为绿的对应方向*/
private String opposite;
/*当前灯变红时下一个变绿的灯*/
private String next;
public boolean isLighted(){
return lighted;
}
/**
* 某个灯变绿时,它对应方向的灯也要变绿
*/
public void light(){
this.lighted = true;
if(opposite != null){
Lamp.valueOf(opposite).light();
}
System.out.println(name() + " lamp is green,下面总共应该有6个方向能看到汽车穿过!");
}
/**
* 某个灯变红时,对应方向的灯也要变红,并且下一个方向的灯要变绿
* @return 下一个要变绿的灯
*/
public Lamp blackOut(){
this.lighted = false;
if(opposite != null){
Lamp.valueOf(opposite).blackOut();
}
Lamp nextLamp= null;
if(next != null){
nextLamp = Lamp.valueOf(next);
System.out.println("绿灯从" + name() + "-------->切换为" + next);
nextLamp.light();
}
return nextLamp;
}
}The above is the detailed introduction to the use of enumerations in Java. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!
Hot AI Tools
Undress AI Tool
Undress images for free
AI Clothes Remover
Online AI tool for removing clothes from photos.
Undresser.AI Undress
AI-powered app for creating realistic nude photos
ArtGPT
AI image generator for creative art from text prompts.
Stock Market GPT
AI powered investment research for smarter decisions
Hot Article
Popular tool
Notepad++7.3.1
Easy-to-use and free code editor
SublimeText3 Chinese version
Chinese version, very easy to use
Zend Studio 13.0.1
Powerful PHP integrated development environment
Dreamweaver CS6
Visual web development tools
SublimeText3 Mac version
God-level code editing software (SublimeText3)
Hot Topics
20446
7
13592
4
How to fix the erroneous display of IE browser pages? IE browser compatibility settings
Jan 09, 2026 pm 09:18 PM
The main cause of IE page confusion is a mismatch in rendering modes, which can be repaired in five steps: 1. Enable compatibility view; 2. Manually switch document mode; 3. Reset IE settings; 4. Disable third-party add-ons; 5. Check group policy restrictions.
How to set system environment variables on Windows 10 computer_Win10 advanced system settings Path configuration
Jan 10, 2026 am 06:30 AM
It is necessary to configure the Windows 10 system Path environment variable to enable the command line to recognize the program. The methods include: 1. Setting the graphical interface; 2. Running sysdm.cpl for quick entry; 3. Searching for "Edit System Environment Variables" on the Start menu; 4. The PowerShell administrator executes SetEnvironmentVariable; 5. Temporarily adding the set command in cmd.
Devin AI: Disrupting software engineering? An in-depth analysis of the future of AI software engineers
Jan 12, 2026 pm 10:54 PM
In 2025, artificial intelligence (AI) is penetrating into various industries at an alarming rate, and the field of software engineering is no exception. Recently, an AI software engineer named DevinAI emerged, which has attracted widespread attention in the industry. DevinAI claims to be able to complete software development tasks autonomously, which has triggered concerns and discussions about whether AI will replace traditional software engineers. This article
How to embed web pages in PPT for real-time display_Insert using add-ins or development tool controls
Jan 14, 2026 pm 07:09 PM
There are three ways to embed real-time web content in PowerPoint: 1. Use the Microsoft WebBrowser ActiveX control (Windows only); 2. Install a third-party LiveWeb add-in; 3. Add a verified official add-in like "WebViewer" from the Office App Store.
Google Chrome directly opens the Google Chrome web page access version link
Jan 14, 2026 pm 10:18 PM
The link to enter the Google Chrome web page is https://www.google.com/chrome/. This page has five core features: simple interface, stable multi-device synchronization, rich extension ecosystem, complete security protection, and strong media playback compatibility.
Quark Browser video cannot be played. Steps to troubleshoot Quark Browser playback problems.
Jan 22, 2026 am 09:30 AM
Solutions to abnormal video playback in Quark browser include: 1. Check network connection and access permissions; 2. Clear cache and website data; 3. Enable JavaScript and H5 playback support; 4. Switch to desktop version of UA; 5. Turn off hardware acceleration and reset media services.
How to adjust Safari browser search settings when search results are abnormal in Safari browser
Jan 10, 2026 pm 12:30 PM
If Safari search results deviate from expectations, adjustments should be made in sequence: 1. Switch to the adapted default search engine; 2. Enable intelligent search suggestions and train local preferences; 3. Clear problematic website data and reset personalization; 4. Use advanced syntax such as quotation marks and minus signs for precise queries; 5. Reset search preferences and reconfigure.
GitHub Copilot X: AI programming revolution, doubled efficiency!
Jan 12, 2026 pm 10:45 PM
In the field of software development, artificial intelligence (AI) is gradually becoming a force that cannot be ignored. GitHubCopilotX, as an AI-assisted programming tool, is changing the programmer's workflow with its powerful functions and convenient use. It can not only automatically generate code based on context, but also perform code interpretation, generate unit tests and create documentation, greatly improving development




