1 00:00:00,470 --> 00:00:02,450 Objects can be immutable or immutable. 2 00:00:07,240 --> 00:00:10,240 In the past, we separated primitive types from class types. 3 00:00:14,360 --> 00:00:18,860 We can also separate class types into two categories, immutable and immutable. 4 00:00:24,000 --> 00:00:28,080 In this lesson, you're to learn to tell the difference between mutable and immutable objects. 5 00:00:31,730 --> 00:00:36,110 To access this lesson, follow this path and your resources and open up the mutable folder. 6 00:00:44,690 --> 00:00:50,960 Now, another name for Setar is Mutator before we continue to take some time to absorb this sentence. 7 00:01:00,600 --> 00:01:04,319 Immutable object is an object whose field values can change. 8 00:01:06,100 --> 00:01:10,840 Immutable object uses Mutator is Setas to update its fields. 9 00:01:15,240 --> 00:01:19,110 And so inside Maine, the first line creates an object of the car class. 10 00:01:19,650 --> 00:01:23,380 Let me ask you this, is car a mutable or an immutable class type? 11 00:01:24,150 --> 00:01:25,800 Take a couple of seconds to think about it. 12 00:01:33,190 --> 00:01:38,350 The answer is mutable after you create a car object, it's possible to update it. 13 00:01:38,740 --> 00:01:44,200 In other words, the mutable object can use its mutator as its setas to update its fields. 14 00:01:49,410 --> 00:01:55,200 And so to keep things simple at its core, what makes an object immutable after creating the object, 15 00:01:55,200 --> 00:01:57,410 Wicken updated state, that's it. 16 00:01:57,430 --> 00:01:58,560 We can mutate it. 17 00:02:03,220 --> 00:02:09,190 Now, what is an immovable object, an immutable object is an object whose field values cannot change, 18 00:02:09,729 --> 00:02:13,540 an immutable object does not have Mutator doesn't have Setas. 19 00:02:16,550 --> 00:02:20,840 And you can access this part of the lesson by following this path and the resources and open up the 20 00:02:20,840 --> 00:02:21,830 immutable folder. 21 00:02:30,290 --> 00:02:33,980 Can I go inside, Main Espersen, immutable or an immutable class type? 22 00:02:34,370 --> 00:02:34,940 Think about it. 23 00:02:43,120 --> 00:02:48,880 The answer is immutable after you create a personal object, it's impossible to update it. 24 00:02:53,840 --> 00:02:58,470 All of the fields are private and the person class doesn't have any setas. 25 00:02:59,090 --> 00:03:02,300 This makes every object of the person class immutable. 26 00:03:02,810 --> 00:03:06,940 It's impossible to update the state of a person object after you create it. 27 00:03:10,870 --> 00:03:16,240 This leads us to the reference chart, mutable objects are vulnerable to the reference trap. 28 00:03:18,580 --> 00:03:21,910 Immutable objects are immune to the reference trap. 29 00:03:23,500 --> 00:03:25,780 So these two tables don't make any sense. 30 00:03:26,260 --> 00:03:32,830 The reference job doesn't apply to immutable objects, so the tables need to specify that mutable objects 31 00:03:32,830 --> 00:03:35,210 are the ones that are prone to the reference trap. 32 00:03:35,680 --> 00:03:36,460 Let's do just that. 33 00:03:38,160 --> 00:03:39,570 OK, this feels better. 34 00:03:45,460 --> 00:03:49,550 You now inside main job, we're going to set another variable equal to the first object. 35 00:03:50,620 --> 00:03:53,350 And then I'm going to update the object from the second variable. 36 00:03:57,960 --> 00:04:00,240 Then I'll print the gold from both objects. 37 00:04:06,940 --> 00:04:07,720 Run the code. 38 00:04:15,210 --> 00:04:19,890 And unsurprisingly, updating the state of the second variable affects the first one. 39 00:04:24,090 --> 00:04:29,220 When you set a variable equal to another, it copies the value inside in this case, what's inside is 40 00:04:29,220 --> 00:04:34,040 a reference, and now both variables share a reference that points to the same object. 41 00:04:34,470 --> 00:04:37,800 If I update the object there, one variable, it's going to affect the other. 42 00:04:38,940 --> 00:04:42,250 And so mutable objects are vulnerable to the reference trap. 43 00:04:42,750 --> 00:04:44,550 What about immutable objects? 44 00:04:47,450 --> 00:04:51,590 Inside Main Java set another variable equal to the first person object. 45 00:04:56,220 --> 00:04:57,290 OK, what now? 46 00:04:57,720 --> 00:05:02,730 Every object of the person class is immutable, the fields are private and the person class doesn't 47 00:05:02,730 --> 00:05:03,620 have any setas. 48 00:05:03,990 --> 00:05:09,810 So even if both variables share a reference to the same object, there's nothing you can do to update 49 00:05:09,810 --> 00:05:10,860 the state of either one. 50 00:05:11,700 --> 00:05:13,010 Let me just put breakpoints. 51 00:05:13,030 --> 00:05:14,340 You can visualize the runtime. 52 00:05:16,400 --> 00:05:21,920 And so here, both variables share a reference to the same object, but that doesn't matter because 53 00:05:21,920 --> 00:05:23,560 we can't update either one. 54 00:05:24,020 --> 00:05:27,620 All you can do is set the second variable equal to a brand new object. 55 00:05:47,510 --> 00:05:52,750 But in that case, the second variable story is a new reference that points to a unique object. 56 00:05:53,420 --> 00:05:57,890 And so I hope you can see that immutable objects are immune to the reference trap. 57 00:06:04,690 --> 00:06:09,940 OK, so first we need to update this table, because we can also divide class types into two categories, 58 00:06:09,940 --> 00:06:11,440 mutable and immutable. 59 00:06:15,190 --> 00:06:22,090 Mutable objects can update their status, immutable objects cannot mutable objects are vulnerable to 60 00:06:22,090 --> 00:06:25,150 the reference Tropp immutable objects are immune. 61 00:06:33,650 --> 00:06:38,000 Array is a mutable object, there is a class for every array type. 62 00:06:38,450 --> 00:06:43,700 There's a class for array of integers, array of double array of long, etc.. 63 00:06:45,490 --> 00:06:50,080 So looking at this example, the variable equals the new object of the entire class. 64 00:06:53,630 --> 00:07:00,560 The variable stores a reference to an object, the variable can be no, it can call methods, you can 65 00:07:00,560 --> 00:07:05,060 update the state of an array, and arrays are vulnerable to the reference trap. 66 00:07:09,180 --> 00:07:13,710 And since arrays are mutable objects, we don't need these two tables, the Redundance. 67 00:07:16,480 --> 00:07:17,140 Remove them. 68 00:07:18,210 --> 00:07:19,760 And this looks a lot better. 69 00:07:29,160 --> 00:07:33,480 Let's recap, you learn to tell the difference between mutable and immutable objects. 70 00:07:35,200 --> 00:07:40,840 You can update the state of immutable object, but you can't update the state of an immutable object.