Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
student:java:enum [2023/02/20 15:31] – [With thanks to Dr. Bernstein...] stewarmcstudent:java:enum [2023/02/24 14:13] (current) – [Enum Types] stewarmc
Line 71: Line 71:
 </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 &lt; 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",
Line 90: Line 90:
 **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 {
Line 128: Line 128:
         double mass = earthWeight/EARTH.surfaceGravity();         double mass = earthWeight/EARTH.surfaceGravity();
         Planet[] planetValues = Planet.values();         Planet[] planetValues = Planet.values();
-        for (int i = 0; i &lt; 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",
Line 137: Line 137:
 </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