Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
student:java:enum [2023/02/20 12:34] – created stewarmc | student:java:enum [2023/02/24 14:13] (current) – [Enum Types] stewarmc | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | placeholder | + | =====Enum Types===== |
+ | Note: This reading has been modified from [[https:// | ||
+ | |||
+ | An '' | ||
+ | |||
+ | Because they are constants, the names of an enum type's fields are in uppercase letters. | ||
+ | |||
+ | In the Java programming language, you define an enum type by using the '' | ||
+ | |||
+ | <code java> | ||
+ | public enum Day { | ||
+ | SUNDAY, MONDAY, TUESDAY, WEDNESDAY, | ||
+ | THURSDAY, FRIDAY, SATURDAY | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | You should use enum types any time you need to represent a fixed set of constants. That includes natural enum types such as the planets in our solar system and data sets where you know all possible values at compile time& | ||
+ | |||
+ | Here is some code that shows you how to use the '' | ||
+ | |||
+ | <code java> | ||
+ | public class EnumTest { | ||
+ | Day day; | ||
+ | |||
+ | public EnumTest(Day day) { | ||
+ | this.day = day; | ||
+ | } | ||
+ | |||
+ | public void tellItLikeItIs() { | ||
+ | switch (day) { | ||
+ | case MONDAY: | ||
+ | System.out.println(" | ||
+ | break; | ||
+ | |||
+ | case FRIDAY: | ||
+ | System.out.println(" | ||
+ | break; | ||
+ | |||
+ | case SATURDAY: case SUNDAY: | ||
+ | System.out.println(" | ||
+ | break; | ||
+ | |||
+ | default: | ||
+ | System.out.println(" | ||
+ | break; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | EnumTest firstDay = new EnumTest(Day.MONDAY); | ||
+ | firstDay.tellItLikeItIs(); | ||
+ | EnumTest thirdDay = new EnumTest(Day.WEDNESDAY); | ||
+ | thirdDay.tellItLikeItIs(); | ||
+ | EnumTest fifthDay = new EnumTest(Day.FRIDAY); | ||
+ | fifthDay.tellItLikeItIs(); | ||
+ | EnumTest sixthDay = new EnumTest(Day.SATURDAY); | ||
+ | sixthDay.tellItLikeItIs(); | ||
+ | EnumTest seventhDay = new EnumTest(Day.SUNDAY); | ||
+ | seventhDay.tellItLikeItIs(); | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | The output is: | ||
+ | <code java> | ||
+ | Mondays are bad. | ||
+ | Midweek days are so-so. | ||
+ | Fridays are better. | ||
+ | Weekends are best. | ||
+ | Weekends are best. | ||
+ | </ | ||
+ | |||
+ | Java programming language enum types are much more powerful than their counterparts in other languages. The '' | ||
+ | |||
+ | <code java> | ||
+ | Planet[] planetValues = Planet.values(); | ||
+ | for (int i = 0; i < planetValues.length; | ||
+ | Planet p = planetValues[i]; | ||
+ | System.out.printf(" | ||
+ | p, p.surfaceWeight(mass)); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | **Note: //All//** enums implicitly extend '' | ||
+ | |||
+ | In the following example, '' | ||
+ | |||
+ | Each enum constant is declared with values for the mass and radius parameters. These values are passed to the constructor when the constant is created. Java requires that the constants be defined first, prior to any fields or methods. Also, when there are fields and methods, the list of enum constants must end with a semicolon. | ||
+ | |||
+ | **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 java> | ||
+ | public enum Planet { | ||
+ | MERCURY (3.303e+23, 2.4397e6), | ||
+ | VENUS | ||
+ | EARTH | ||
+ | MARS (6.421e+23, 3.3972e6), | ||
+ | JUPITER (1.9e+27, | ||
+ | SATURN | ||
+ | URANUS | ||
+ | NEPTUNE (1.024e+26, 2.4746e7); | ||
+ | |||
+ | private final double mass; // in kilograms | ||
+ | private final double radius; // in meters | ||
+ | Planet(double mass, double radius) { | ||
+ | this.mass = mass; | ||
+ | this.radius = radius; | ||
+ | } | ||
+ | private double mass() { return mass; } | ||
+ | private double radius() { return radius; } | ||
+ | |||
+ | // universal gravitational constant | ||
+ | public static final double G = 6.67300E-11; | ||
+ | |||
+ | double surfaceGravity() { | ||
+ | return G * mass / (radius * radius); | ||
+ | } | ||
+ | double surfaceWeight(double otherMass) { | ||
+ | return otherMass * surfaceGravity(); | ||
+ | } | ||
+ | public static void main(String[] args) { | ||
+ | if (args.length != 1) { | ||
+ | System.err.println(" | ||
+ | System.exit(-1); | ||
+ | } | ||
+ | double earthWeight = Double.parseDouble(args[0]); | ||
+ | double mass = earthWeight/ | ||
+ | Planet[] planetValues = Planet.values(); | ||
+ | for (int i = 0; i < planetValues.length; | ||
+ | Planet p = planetValues[i]; | ||
+ | System.out.printf(" | ||
+ | p, p.surfaceWeight(mass)); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | If you run '' | ||
+ | < | ||
+ | $ java Planet 175 | ||
+ | Your weight on MERCURY is 66.107583 | ||
+ | Your weight on VENUS is 158.374842 | ||
+ | Your weight on EARTH is 175.000000 | ||
+ | Your weight on MARS is 66.279007 | ||
+ | Your weight on JUPITER is 442.847567 | ||
+ | Your weight on SATURN is 186.552719 | ||
+ | Your weight on URANUS is 158.397260 | ||
+ | Your weight on NEPTUNE is 199.207413 | ||
+ | </ | ||
+ | |||
+ | =====With thanks to Dr. Bernstein...===== | ||
+ | |||
+ | The following was [[https:// | ||
+ | |||
+ | Though it is not immediately obvious from the documentation for the [[https:// | ||
+ | |||
+ | * Static Methods: | ||
+ | * '' | ||
+ | * '' | ||
+ | * Non-Static Methods: | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' |