SlideShare a Scribd company logo
Java Enum
Sujit Kumar
Zenolocity LLC
Copyright @ 2012 - 2024
Enums
• An enum type is a type whose fields consist of
a fixed set of constants.
• The names of an enum type's fields are in
uppercase letters.
• Example:
public enum Day {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY, SATURDAY
}
When to use Enums
• Use an Enum any time you need to represent
a fixed set of constants.
• Examples:
Months of Year
Days of Week
Entries in a Menu
Colors
Directions of Compass
Enums (contd…)
• Enums are like classes, they can have fields
(state) and methods (behavior).
• All enums implicitly extend java.lang.Enum.
Since Java does not support multiple
inheritance, an enum cannot extend anything
else.
• Typesafe => compile-time error checking.
• You can use a switch on enum values.
Enums (contd…)
• toString() returns the enum value as a
String — can be overridden.
• values() method returns an array of all the
enum values.
• Can be used in collections as objects more
efficiently than int values — no
boxing/unboxing required!
• Implement the Comparable and Serializable
interfaces.
High Performance Set and Map
implementations for Enums
• EnumSet
All of the members of an EnumSet must be of the same
enum type.
Supports iteration over a range of enum types.

• EnumMap
Almost the same speed as an array.
If you want to map an enum to a value, use an EnumMap
instead of an array.
EnumMap's implementation uses an array and for that
reason has slightly better performance than HashMaps.

More Related Content

PPTX
Rip presentation
PPT
Java Arrays
PPTX
Wireless transmission
PPTX
Enumerations in java.pptx
PPTX
Dns 2
PPTX
IPv6 address
PPTX
OSPF Presentation
PPTX
Imap server
Rip presentation
Java Arrays
Wireless transmission
Enumerations in java.pptx
Dns 2
IPv6 address
OSPF Presentation
Imap server

What's hot (20)

PPTX
Java tokens
DOC
Jumping statements
PPT
Constants in C Programming
PDF
Looping in c language
PPT
enums
DOCX
Frame relay
PPTX
Array in Java
PPTX
Arrays in java
PPTX
what is java?
PPTX
Classless inter domain routing
PPTX
Mobile transport layer .
PDF
Strings in java
PDF
ITFT-Constants, variables and data types in java
PPTX
constructors in java ppt
PPTX
Web services protocols
PPT
Guided Transmission Media
PDF
itft-Decision making and branching in java
DOCX
Questions from chapter 1 data communication and networking
Java tokens
Jumping statements
Constants in C Programming
Looping in c language
enums
Frame relay
Array in Java
Arrays in java
what is java?
Classless inter domain routing
Mobile transport layer .
Strings in java
ITFT-Constants, variables and data types in java
constructors in java ppt
Web services protocols
Guided Transmission Media
itft-Decision making and branching in java
Questions from chapter 1 data communication and networking
Ad

Viewers also liked (7)

PPTX
Debug a java program
PPTX
Java dates
PPTX
Introduction to java exceptions
PPTX
Java interfaces
PPT
Java Web Development Course
PPTX
Java final keyword
PPTX
Java file paths
Debug a java program
Java dates
Introduction to java exceptions
Java interfaces
Java Web Development Course
Java final keyword
Java file paths
Ad

Similar to Java enum (20)

PPTX
PPTX
Lecture 6 Enumeration in java ADVANCE.pptx
PPT
05a-enum.ppt
PPT
Effective Java - Enum and Annotations
PDF
c# Enumerations
PDF
Enumeration in Java Explained | Java Tutorial | Edureka
PPTX
21CS642 Module 1 Enumerations PPT.pptx VI SEM CSE 2021 Batch Students
ODP
Enum - Coding Guidelines
PPTX
Java essence part 1
PPTX
Enumerated data types
PPTX
Enumeration In JAVA
PPTX
Proper_Enums_in_TypeScript_enums_best.pptx
PDF
Enum map
PDF
Diving in OOP (Day 6): Understanding Enums in C# (A Practical Approach)
PPTX
8enum in c#
PPT
MODULE 1 EnumerationSSP. for adavanced lecture
PDF
Tiger: Java 5 Evolutions
PPTX
Enums_in_TypeScript_one_of_the_best_ppt.pptx
PDF
ENUM - make u r names as data types
PPTX
Enum
Lecture 6 Enumeration in java ADVANCE.pptx
05a-enum.ppt
Effective Java - Enum and Annotations
c# Enumerations
Enumeration in Java Explained | Java Tutorial | Edureka
21CS642 Module 1 Enumerations PPT.pptx VI SEM CSE 2021 Batch Students
Enum - Coding Guidelines
Java essence part 1
Enumerated data types
Enumeration In JAVA
Proper_Enums_in_TypeScript_enums_best.pptx
Enum map
Diving in OOP (Day 6): Understanding Enums in C# (A Practical Approach)
8enum in c#
MODULE 1 EnumerationSSP. for adavanced lecture
Tiger: Java 5 Evolutions
Enums_in_TypeScript_one_of_the_best_ppt.pptx
ENUM - make u r names as data types
Enum

More from Sujit Kumar (20)

PPTX
Introduction to OOP with java
PPTX
SFDC Database Basics
PPTX
SFDC Database Security
PPTX
SFDC Social Applications
PPTX
SFDC Other Platform Features
PPTX
SFDC Outbound Integrations
PPTX
SFDC Inbound Integrations
PPTX
SFDC UI - Advanced Visualforce
PPTX
SFDC UI - Introduction to Visualforce
PPTX
SFDC Deployments
PPTX
SFDC Batch Apex
PPTX
SFDC Data Loader
PPTX
SFDC Advanced Apex
PPTX
SFDC Introduction to Apex
PPTX
SFDC Database Additional Features
PPTX
Introduction to SalesForce
PPTX
More about java strings - Immutability and String Pool
PPTX
Hibernate First and Second level caches
PPTX
Java equals hashCode Contract
PPTX
Java Comparable and Comparator
Introduction to OOP with java
SFDC Database Basics
SFDC Database Security
SFDC Social Applications
SFDC Other Platform Features
SFDC Outbound Integrations
SFDC Inbound Integrations
SFDC UI - Advanced Visualforce
SFDC UI - Introduction to Visualforce
SFDC Deployments
SFDC Batch Apex
SFDC Data Loader
SFDC Advanced Apex
SFDC Introduction to Apex
SFDC Database Additional Features
Introduction to SalesForce
More about java strings - Immutability and String Pool
Hibernate First and Second level caches
Java equals hashCode Contract
Java Comparable and Comparator

Recently uploaded (20)

PDF
cuic standard and advanced reporting.pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
Tartificialntelligence_presentation.pptx
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
Spectroscopy.pptx food analysis technology
PDF
Machine learning based COVID-19 study performance prediction
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Accuracy of neural networks in brain wave diagnosis of schizophrenia
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
Big Data Technologies - Introduction.pptx
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPTX
1. Introduction to Computer Programming.pptx
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
cuic standard and advanced reporting.pdf
Building Integrated photovoltaic BIPV_UPV.pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
Unlocking AI with Model Context Protocol (MCP)
Tartificialntelligence_presentation.pptx
Dropbox Q2 2025 Financial Results & Investor Presentation
20250228 LYD VKU AI Blended-Learning.pptx
Spectroscopy.pptx food analysis technology
Machine learning based COVID-19 study performance prediction
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
The Rise and Fall of 3GPP – Time for a Sabbatical?
MYSQL Presentation for SQL database connectivity
Accuracy of neural networks in brain wave diagnosis of schizophrenia
Programs and apps: productivity, graphics, security and other tools
Big Data Technologies - Introduction.pptx
A comparative analysis of optical character recognition models for extracting...
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
1. Introduction to Computer Programming.pptx
Group 1 Presentation -Planning and Decision Making .pptx

Java enum

  • 1. Java Enum Sujit Kumar Zenolocity LLC Copyright @ 2012 - 2024
  • 2. Enums • An enum type is a type whose fields consist of a fixed set of constants. • The names of an enum type's fields are in uppercase letters. • Example: public enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }
  • 3. When to use Enums • Use an Enum any time you need to represent a fixed set of constants. • Examples: Months of Year Days of Week Entries in a Menu Colors Directions of Compass
  • 4. Enums (contd…) • Enums are like classes, they can have fields (state) and methods (behavior). • All enums implicitly extend java.lang.Enum. Since Java does not support multiple inheritance, an enum cannot extend anything else. • Typesafe => compile-time error checking. • You can use a switch on enum values.
  • 5. Enums (contd…) • toString() returns the enum value as a String — can be overridden. • values() method returns an array of all the enum values. • Can be used in collections as objects more efficiently than int values — no boxing/unboxing required! • Implement the Comparable and Serializable interfaces.
  • 6. High Performance Set and Map implementations for Enums • EnumSet All of the members of an EnumSet must be of the same enum type. Supports iteration over a range of enum types. • EnumMap Almost the same speed as an array. If you want to map an enum to a value, use an EnumMap instead of an array. EnumMap's implementation uses an array and for that reason has slightly better performance than HashMaps.