The main objective of enum is to define our own data types Enumerated Data Types. Attention reader! Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. Declaration of enum in java : Enum declaration can be done outside a Class or inside a Class but not inside a Method.
RED; System. According to Java naming conventions , it is recommended that we name constant with all capital letters Important points of enum : Every enum is internally implemented by using Class. Java import java.
Every enum constant is always implicitly public static final. Since it is static , we can access it by using the enum Name. We can declare the main method inside the enum. Hence we can invoke enum directly from the Command Prompt.
Enum class. As a class can only extend one parent in Java, so an enum cannot extend anything else. Enum class , which returns enum constant name. Order is important in enums. To create an enum , use the enum keyword instead of class or interface , and separate the constants with a comma. Note that they should be in uppercase letters:. You can access enum constants with the dot syntax:.
Enums are often used in switch statements to check for corresponding values:. The enum type has a values method, which returns an array of all enum constants. This method is useful when you want to loop through the constants of an enum:. Enums help developers store data that they know will not change. Or, you decide a variable that stores employee contracts can only store Part-time , Full-time , or Zero-hours.
In these cases, you would want to use an enum to store data. Get matched to a bootcamp today. The average bootcamp grad spent less than six months in career transition, from starting a bootcamp to finding their first job. Using enums allows you to express an algorithm in a more readable way to both yourself and the computer. Writing an enum tells the computer a variable can only have a specific number of values. It also tells you, the coder, that this is the case, which will make it easier to understand your code.
If you see a variable that uses an enum, you know that the variable can only have one of a limited number of values. Additionally, enums allow you to more effectively use constants. In fact, enum was introduced to replace int constants in Java, which spanned multiple lines and were difficult to read. Our code takes up five lines to declare these constants. But by using an enum, we can reduce our code to three lines. In the example below, we declare a list of enum constants:. We are building an app that wait staff can use to submit a coffee order to the baristas at a coffee shop.
When a barista inserts a value for the size of the drink, we only want there to be three possible options. We could use an enum to limit the possible sizes of a drink to those options:. In this example, we have declared an enum called Sizes which has three possible values.
Because they are constants, the names of an enum type's fields are in uppercase letters. Mondays are bad. Midweek days are so-so. Fridays are better. Weekends are best.
Note: All enums implicitly extend java. Because a class can only extend one parent see Declaring Classes , the Java language does not support multiple inheritance of state see Multiple Inheritance of State, Implementation, and Type , and therefore an enum cannot extend anything else. Note: The constructor for an enum type must be package-private or private access. It automatically creates the constants that are defined at the beginning of the enum body.
0コメント