2007-04-25

Enum Types (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

Enum Types (The Java™ Tutorials > Learning the Java Language > Classes and Objects): "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 the for-each construct 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.

for (Planet p : Planet.values()) {
System.out.printf('Your weight on %s is %f%n',
p, p.surfaceWeight(mass));
}"

0 comments: