When was enum added to java




















Related Articles. Basics of Java. Operators in Java. Packages in Java. Flow Control in Java. Loops in Java. Jump Statements in Java. Arrays in Java. Strings in Java. OOPS in Java. Constructors in Java.

Interfaces in Java. Keywords in Java. Exception Handling in Java. Collection Framework. Multi-threading in Java. Table of Contents. Save Article. Improve Article. Like Article. Here is a sample program that takes your weight on earth in any unit and calculates and prints your weight on all of the planets in the same unit :. If you run Planet. All rights reserved.

Hide TOC. Classes and Objects. Passing Information to a Method or a Constructor. Summary of Creating and Using Classes and Objects. 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. Notice the abstract method declaration at the bottom of the enum class. Notice also how each enum instance each constant defines its own implementation of this abstract method. Using an abstract method is useful when you need a different implementation of a method for each instance of a Java enum. A Java Enum can implement a Java Interface in case you feel that makes sense in your situation. Here is an example of a Java Enum implementing an interface:.

It is the method getDescription that comes from the interface MyInterface. Implementing an interface with an Enum could be used to implement a set of different Comparator constants which can be used to sort collections of objects. Java contains a special Java Set implementation called EnumSet which can hold enums more efficiently than the standard Java Set implementations.

Here is how you create an instance of an EnumSet :. Java also contains a special Java Map implementation which can use Java enum instances as keys. Here is a Java EnumMap example:. Java enums extend the java. Enum class implicitly, so your enum types cannot extend another class. If a Java enum contains fields and methods, the definition of fields and methods must always come after the list of constants in the enum.

Additionally, the list of enum constants must be terminated by a semicolon;. Tutorials About RSS. Java Language. Enums in if Statements Since Java enums are constants you will often have to compare a variable pointing to an enum constant against the possible constants in the enum type. Enums in switch Statements If your Java enum types contain a lot constants and you need to check a variable against the values as shown in the previous section, using a Java switch statement might be a good idea.

Enum Iteration You can obtain an array of all the possible values of a Java enum type by calling its static values method. Here is an example of iterating all values of an enum: for Level level : Level. Enum toString An enum class automatically gets a toString method in the class when compiled.

Enum Printing If you print an enum, like this: System. HIGH ; Then the toString method will get called behind the scenes, so the value that will be printed out is the textual name of the enum instance. Enum valueOf An enum class automatically gets a static valueOf method in the class when compiled.

Enum Fields You can add fields to a Java enum. Enum Methods You can add methods to a Java enum too. HIGH; System. Enum Abstract Methods It is possible for a Java enum class to have abstract methods too.



0コメント

  • 1000 / 1000