Loading topic
switch handles that job. This chapter shows the classic switch statement, the newer arrow style, and the switch expression, which can check that you handled every constant.2 min readDay enum and used it in a switch. Now you will store enum constants in collections. A collection is an object that holds a group of values, like a list or a set. Java has two collections built just for enums: EnumSet and EnumMap.2 min readCoin stored its own number of cents, but each coin ran the same cents() code. Sometimes each constant needs to do something different. Java lets you give every constant its own version of a method. This is called **constant-spe2 min readnew Range(10, 2), where the start comes after the end. This chapter shows you how to reject bad data, and how to clean it up, before the record is created.2 min readequals, hashCode, and toString for free. You can also add your own code to the record body. This chapter shows two kinds of code you can add: instance methods and static factory methods.2 min readswitch with enum constants, and you built a sealed interface whose subtypes are records. This chapter brings those two ideas together. You need JDK 21 or later to run the code.2 min read