Statements Updated for Enumerations

Two statements have been changed to make them work better with enumerations: the switch statement, and the for statement.

Using a for statement with enums

Language designers want to make it easy to express the concept “Iterate through all the values in this enumeration type”. There was much discussion about how to modify the “for” loop statement. It was done in a clever way—by adding support for iterating through all the values in any array.

Here is the new “get all elements of an array” syntax added to the “for” statement.

// for each Dog object in the dogsArray[] print out the dog's namefor (Dog dog : dogsArray ){    dog.printName();}

This style of “for” statement has become known as the “foreach” ...

Get Just Java™ 2 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.