Enum is a special class that represents constants. It provides type-safe checking. For example, we could create a method that will accept only specific values and for other values, it will give a compilation error. Without enum, we would find out about the wrong value during the runtime only.

  • Enum syntax
  • Enum with methods and switch statement
  • Advance enum

Enum syntax

  • Enum is a special class for constants so to create the enum we need to write enum instead of class.
  • In the body of enum, we can just list all constant values enum should have.
  • As a constant, the values of enum go in uppercase.
  • We can get enum by using its name and actual constant value.
  • We can create a variable that can hold enum.
  • The enum has few methods. In the above example, I’m showing two of them.
  • values() — return all values of enum as an array
  • valueOf(String) — it’s possible to get enum from a string as well.
  • We can check enums on equality with == operator and with equals() method.

Enum with methods and switch statement

  • above method was designed without enum. The method accepts a string as a day and the method will print a message. Note, if client code will provide wrong input, the method will throw a runtime exception. It would be great to catch wrong input during the compilation stage. We can use our enum to do so.
  • When using Enum we are making sure that client code never can provide wrong input. It will not just simply compile.
  • The switch statement can be used with Enum. Notice, the case value does not have an enum name but just its value.

Advance Enum

It’s always good to keep enum as a simple list of constant values. However, sometimes you might need extra features for your enum. Enum can have constructors, fields, and methods.

  • Notice, we have ; after list of values. Without it will not compile.
  • Enum can have only private constructors. It can be used inside the enum only. When declaring our constant enums we need to provide value for our constructor.

This is it for Enum in Java. Have a nice day!

1. Introduction to programming 
2. OS, File, and File System
3. Working with terminal
4. Welcome to Java Programming Language
5. Variables and Primitives in Java
6. Methods with Java
7. Java Math Operators and special operators
8. Conditional branching in Java
9. Switch statement in Java
10. Ternary operator in Java
11. Enum in Java
12. String class and its methods in Java
13. Loops in Java
14. Access modifiers in Java
15. Static keyword in Java
16. The final keyword in Java
17. Class and Object in Java
18. Object Oriented Programming in Java
19. OOP: Encapsulation in Java
20. Inheritance in Java
21. Abstraction in Java
22. Polymorphism in Java
23. Overriding vs Overloading in Java
24. OOP Design Principles in Java
25. Array in Java
26. Data Structures with Java
27. Collection framework in Java
28. ArrayList in Java
29. Set in Java
30. Map in Java
31. LocalDate in Java
32. Exception in Java
33. IO in Java
34. Design Patterns
35. Generics in Java
36. Multithreading in java
37. JUnit
38. Big O Notation for coding interviews
39. Top 17 Java coding interview questions for SDET

Software Developer, Java Instructor https://www.techleadacademy.io/

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store