1 00:00:00,930 --> 00:00:03,870 An item contains a limited number of constants. 2 00:00:05,260 --> 00:00:09,790 Using enum to ensure something can only be a limited number of values. 3 00:00:12,070 --> 00:00:13,720 In this lesson, you're going to create an enum. 4 00:00:15,900 --> 00:00:19,590 An enum defines a collection of static final constants. 5 00:00:22,250 --> 00:00:26,900 Using Inam to ensure a variable can only be a limited number of values. 6 00:00:30,180 --> 00:00:34,970 The size of a shirt should be limited to three values, small, medium or large. 7 00:00:35,400 --> 00:00:37,590 But what if the caller writes something like Wambo? 8 00:00:38,040 --> 00:00:41,430 They surely can assizes of type string so it can be anything. 9 00:00:42,060 --> 00:00:43,550 Then you might think that's OK. 10 00:00:43,560 --> 00:00:47,040 We could just apply exception handling if the caller writes anything bad. 11 00:00:49,150 --> 00:00:50,980 Yeah, but why do any of that? 12 00:00:51,060 --> 00:00:57,150 From the start, we just use an item to insure this variable can only be a limited number of values. 13 00:00:57,400 --> 00:01:03,070 So inside the shirt class, we're going to create a public item called size that defines three static 14 00:01:03,070 --> 00:01:06,250 final constants, small, medium, large. 15 00:01:10,950 --> 00:01:12,580 This is how you create an item. 16 00:01:12,900 --> 00:01:18,780 All you have to do is make sure to always capitalize constants inside of an item now by default, every 17 00:01:18,780 --> 00:01:20,650 value inside of an enormous final. 18 00:01:21,060 --> 00:01:22,800 These values can never be changed. 19 00:01:23,010 --> 00:01:26,010 Basically, an item is just a collection of final values. 20 00:01:26,940 --> 00:01:31,530 OK, now the problem with the size is that it's a strength, which means it can be any type of value, 21 00:01:31,930 --> 00:01:35,040 we're going to make it an enum type so that it's limited to only three. 22 00:01:36,190 --> 00:01:39,550 And now the constructor needs to receive an item constant. 23 00:01:45,160 --> 00:01:47,770 And now you're going to have to regenerate the getters and setters. 24 00:01:57,980 --> 00:02:04,370 Now, back in Maine, an enum is implicitly static, that means we can access it directly from the class. 25 00:02:06,860 --> 00:02:12,230 And now I can only give the shirt one of three values, small, medium or large, I'm going to give 26 00:02:12,230 --> 00:02:13,280 it a size of small. 27 00:02:17,240 --> 00:02:18,740 And now I can print the object. 28 00:02:31,400 --> 00:02:32,870 And everything works beautifully. 29 00:02:36,520 --> 00:02:39,790 You limited the size field to three possible values. 30 00:02:41,700 --> 00:02:46,290 Use an enum to ensure something can only be a limited number of values.