Sponsored Links
SCJP Tutorials
- SCJP 5 : Chapter 6. Collections / Generics (Part 4)
- SCJP 5 : Chapter 6. Collections / Generics (Part 3)
- SCJP 5 : Chapter 6. Collections / Generics (Part 2)
- SCJP 5 : Chapter 6. Collections / Generics (Part 1)
- SCJP 5 : Chapter 3. API Contents (Part-5)
- SCJP 5 : Chapter 3. API Contents (Part-4)
- SCJP 5 : Chapter 3. API Contents (Part-3)
- SCJP 5 : Chapter 3. API Contents (Part-2)
- SCJP 5 : Chapter 3. API Contents (Part-1)
- SCJP 5 : Chapter 2. Flow Control (Part-5)
- SCJP 5 : Chapter 2. Flow Control (Part-4)
- SCJP 5 : Chapter 2. Flow Control (Part-3)
- SCJP 5 : Chapter 2. Flow Control (Part-2)
- SCJP 5 : Chapter 1. Declarations, Initialization and Scoping (Part-6)
- SCJP 5 : Chapter 2. Flow Control (Part-1)
- SCJP 5 : Chapter 1. Declarations, Initialization and Scoping (Part-5)
- SCJP 5: Chapter 1. Declarations, Initialization and Scoping (Part-4)
- SCJP 5 : Chapter 1. Declarations, Initialization and Scoping (Part-3)
- SCJP 5: Chapter 1. Declarations, Initialization and Scoping (Part-2)
- SCJP 5 : Chapter 1. Declarations, Initialization and Scoping (Part-1)
Certification
SCJPSCJP 5 : Chapter 2. Flow Control (Part-1)
SCJP 5 : Chapter 2. Flow Control (Part-1)
Develop code that implements an if or switch statement; and identify legal argument types for these statements.
The syntax of the switch statement is extended ever-so-slightly. The type of the Expression is now permitted to be an enum class. (Note that java.util.Enum is not an enum class.) A new production is added for SwitchLabel:
SwitchLabel:
.....case EnumConst :
.....
EnumConst:
.....Identifier
The Identifier must correspond to one of UNQUALIFIED enumeration constants.
Here is a slightly more complex enum declaration for an enum type with an explicit instance field and an accessor for this field. Each member has a different value in the field, and the values are passed in via a constructor. In this example, the field represents the value, in cents, of an American coin.
public enum Coin { |
Switch statements are useful for simulating the addition of a method to an enum type from outside the type. This example "adds" a color method to the Coin class, and prints a table of coins, their values, and their colors.
import static java.lang.System.out; |
Running the program prints:
PENNY: 1c COPPER
NICKEL: 5c NICKEL
DIME: 10c SILVER
QUARTER: 25c SILVER
______________
Author: Mikalai Zaikin. Please Click Here to visit Authors site for any updates and changes to the study notes.
Comments
Sponsored Links
