| Both sides previous revisionPrevious revisionNext revision | Previous revision |
| student:java:enum [2023/02/20 15:30] – stewarmc | student:java:enum [2023/02/24 14:13] (current) – [Enum Types] stewarmc |
|---|
| </code> | </code> |
| |
| Java programming language enum types are much more powerful than their counterparts in other languages. The ''%%enum%%'' declaration defines a ''%%class%%'' (called an ''%%enum type%%''). The enum class body can include methods and other fields. The compiler automatically adds some special methods when it creates an enum. For example, they have a static <code>values</code> method that returns an array containing all of the values of the enum in the order they are declared. This method is commonly used in combination with a ''%%for%%'' loop to iterate over the values of an enum type. For example, this code from the ''%%Planet%%'' class example below iterates over all the planets in the solar system. | Java programming language enum types are much more powerful than their counterparts in other languages. The ''%%enum%%'' declaration defines a ''%%class%%'' (called an ''%%enum type%%''). The enum class body can include methods and other fields. The compiler automatically adds some special methods when it creates an enum. For example, they have a static ''%%values%%'' method that returns an array containing all of the values of the enum in the order they are declared. This method is commonly used in combination with a ''%%for%%'' loop to iterate over the values of an enum type. For example, this code from the ''%%Planet%%'' class example below iterates over all the planets in the solar system. |
| |
| <code java> | <code java> |
| Planet[] planetValues = Planet.values(); | Planet[] planetValues = Planet.values(); |
| for (int i = 0; i < planetValues.length; i++) { | for (int i = 0; i < planetValues.length; i++) { |
| Planet p = planetValues[i]; | Planet p = planetValues[i]; |
| System.out.printf("Your weight on %s is %f%n", | System.out.printf("Your weight on %s is %f%n", |
| **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. You cannot invoke an enum constructor yourself. | **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. You cannot invoke an enum constructor yourself. |
| | |
| In addition to its properties and constructor, <code>''%%Planet%%'' has methods that allow you to retrieve the surface gravity and weight of an object on each planet. 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): | In addition to its properties and constructor, ''%%Planet%%'' has methods that allow you to retrieve the surface gravity and weight of an object on each planet. 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): |
| <code java> | <code java> |
| public enum Planet { | public enum Planet { |
| double mass = earthWeight/EARTH.surfaceGravity(); | double mass = earthWeight/EARTH.surfaceGravity(); |
| Planet[] planetValues = Planet.values(); | Planet[] planetValues = Planet.values(); |
| for (int i = 0; i < planetValues.length; i++) { | for (int i = 0; i < planetValues.length; i++) { |
| Planet p = planetValues[i]; | Planet p = planetValues[i]; |
| System.out.printf("Your weight on %s is %f%n", | System.out.printf("Your weight on %s is %f%n", |
| </code> | </code> |
| |
| If you run ''%%Planet.class%%'' from the command line with an argument of 175, you get this output:</p> | If you run ''%%Planet.class%%'' from the command line with an argument of 175, you get this output: |
| <code> | <code> |
| $ java Planet 175 | $ java Planet 175 |
| * Non-Static Methods: | * Non-Static Methods: |
| * ''%%int compareTo(E other)%%'' which uses the order of the elements | * ''%%int compareTo(E other)%%'' which uses the order of the elements |
| * ''%%boolean equals(E other) | * ''%%boolean equals(E other)%%'' |
| * ''%%ordinal()%%'' returns the ordinal value of the instance (0-based) | * ''%%ordinal()%%'' returns the ordinal value of the instance (0-based) |
| * ''%%toString()%%'' returns a ''%%String%%'' representation of the identifier | * ''%%toString()%%'' returns a ''%%String%%'' representation of the identifier |
| |