Enum In Java

What is Enum in Java ?
In a programming language, enumerations are used to represent a set of named constants.Java has a data type called Enum that has a predetermined set of constants.All constants should be written in capital letters in accordance with Java’s naming guidelines. So, we have enum constants in capital letters.It can be used for seasons, directions, days of the week, etc.
Points to remember for Java Enum
- Enum increases type security
- Using enum in switches is simple.
- It is possible to navigate through Enum.
- Fields, constructors, and methods can be found in enum.
- Enum can implement numerous interfaces, but it can only internally extend the Enum class.
Enumeration declaration in Java:
Syntax :
// (Note enum keyword instead of // class keyword) enum Seaon { SPRING, SUMMER, WINTER; } public class Main{ // Driver method public static void main(String[] args) { Seaon s1 = Seaon.SPRING; System.out.println(s1); } }
Output :
SPRING
Explanation :
In this code we have defined an enumerated type Season with three values: SPRING, SUMMER, and WINTER.
In the main method, an instance of Season is created with the value SPRING, which is assigned to the variable s1. The println statement then prints the value of s1, which will be SPRING.
Syntax :
enum Season { SPRING, SUMMER, FALL, WINTER } public class Main { public static void main(String[] args) { Season currentSeason = Season.SPRING; System.out.println("Current season: " + currentSeason); switch (currentSeason) { case SPRING: System.out.println("Flowers are blooming."); break; case SUMMER: System.out.println("It's hot outside."); break; case FALL: System.out.println("Leaves are changing colors."); break; case WINTER: System.out.println("It's cold outside."); break; } } }
Output :
Current season: SPRING Flowers are blooming.
Explanation :
In this example, the enumerated type Season is used to represent the four seasons of the year. In the main method, a variable currentSeason is declared and initialized to Season.SPRING. The println statement then prints the value of currentSeason, which will be SPRING.
The switch statement then uses the currentSeason variable to determine which case to execute. In this case, since currentSeason is equal to Season.SPRING, the message “Flowers are blooming.” will be printed.
Why java Enums ?
Syntax :
class Size { public final static int SMALL = 1; public final static int MEDIUM = 2; public final static int LARGE = 3; public final static int EXTRALARGE = 4; }
If we print the constants in this case, a problem results. This is due to the fact that only the number, which may not be useful, is printed.So, we can just use enums in place of int constants. For example,
Syntax :
enum Size { SMALL, MEDIUM, LARGE, EXTRALARGE }
Java Enum Class Methods :
- Java Enum ordinal()
- Enum compareTo()
- Enum toString()
- Enum name()
- Java Enum valueOf()
- Enum values()
1. Java Enum ordinal() :
Syntax :
ordinal(SMALL) // returns 0
2. Enum compareTo() :
Syntax :
Size.SMALL.compareTo(Size.MEDIUM) // returns ordinal(SMALL) - ordinal(MEDIUM)
3. Enum toString() :
Syntax :
ordinal(SMALL) // returns 0
4.Enum name() :
Syntax :
name(SMALL) // returns "SMALL"
5. Java Enum valueOf() :
Syntax :
Size.valueOf("SMALL") // returns constant SMALL.
6. Java values() :
Syntax :
Size[] enumArray = Size.value();
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment