![]() ![]() Student s1 = new Student("Justin", StudentResult.ABSENT) (this.name + " was absent for the exam.") Įlse if(this.result = StudentResult.PASS) Let's also define a few methods that use the enum data member. We will also this enum type for a data member of the class. ![]() Let's define the StudentResult enum inside this class. In the code below, we have a Student class with a name field. We can also define enum types inside a class and use them as data members for the class. String resultStr = "PRESENT" //Invalid enum constantĮxception in thread "main" : No enum constant StudentResult.PRESENTĪt java.base/(Enum.java:273)Īt Demo.main(Demo.java:13) Enum Inside Classes If the string is invalid or doesn't represent any enum constant, then an IllegalArgumentException is thrown. StudentResult result = StudentResult.valueOf(resultStr) The valueOf() method is used for this conversion. We can also create an enum type variable from a string value. In the previous section, we learned how to convert an enum constant to a string. ("Lowercase: " + resultStr.toLowerCase()) enum StudentResultįor(StudentResult sr : StudentResult.values()) Next, we will convert all of them to lowercase and print them. In the code below, we are first fetching each constant and converting it to a string. This allows us to perform common string operations on the returned value. (sr + " at index " + sr.ordinal()) ĪBSENT at index 2 toString() Method of EnumsĪll enum types get a toString() method that returns the string value of the enum constants. StudentResult resultArr = StudentResult.values() We can also fetch the array index of each constant by using the ordinal() method. We can then traverse this array and fetch individual constants. We can get an array of all enum constants by using the values() method. enum StudentResultĬase ABSENT : ("The Student was absent for the exam.") Ĭase PASS : ("The Student passed the exam.") Ĭase FAIL : ("The Student failed the exam.") ![]() If the number of constants is more, then switch-case statements are a better choice than if-else statements. ![]() ("The student was absent for the exam.") StudentResult result = StudentResult.FAIL StudentResult result3 = StudentResult.PASS Įnums are very frequently used with conditional statements, as they can only have a fixed constant value. StudentResult result2 = StudentResult.FAIL StudentResult result1 = StudentResult.ABSENT We can create a variable of the enum type by using one of the constants. The student could have passed or failed the exam or could have been absent for the exam. Let's create a simple enum that denotes the result of students in an exam. Static allows us to access the constants by using the name of the enum. All enum constants are static and final.All the enum constants must be written in capital letters(NORTH, not north). We use the enum keyword to create an enumeration in Java.An enumeration is created by specifying the constant values that it can have.In this tutorial, we will learn more about enums in Java. For example, an enum of directions can only have four possible values - north, south, east, and west. We can use them to avoid undesirable behavior due to invalid inputs. Enums allow us to know all the possible values for a field at the compile-time and make our code more readable. Enum is a special kind of class that extends the class. In listing 1, the value returned by calling the name() and toString() on an Animal.DOG constant method is DOG.An Enum(short for enumeration) is a special data type in Java that contains a fixed set of constants. The toString() method calls the name() method which returns the string representation of the enum constant. The Java Enum has two methods that retrieve that value of an enum constant, name() and. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |