1 00:00:01,330 --> 00:00:04,030 Once again, we need to be careful of the reference trap. 2 00:00:09,110 --> 00:00:14,240 There are two ways to fall for this trap, it can happen by mishandling arrays and objects. 3 00:00:18,050 --> 00:00:22,010 In this lesson, we're going to see how this trap occurs when mishandling objects. 4 00:00:27,410 --> 00:00:31,780 A trap happens when many variables a reference to one object. 5 00:00:40,400 --> 00:00:43,970 If I update the object through one variable, it's going to affect the other. 6 00:00:50,630 --> 00:00:54,330 To avoid the trap, do not set class variables equal to each other. 7 00:00:54,920 --> 00:00:55,850 Sound familiar? 8 00:00:55,880 --> 00:00:56,590 I hope so. 9 00:00:57,140 --> 00:00:59,390 I told you the same thing about array variables. 10 00:01:03,680 --> 00:01:06,860 The see this inaction will add another variable in Nissan to. 11 00:01:12,280 --> 00:01:14,590 And we'll set it equal to the Nissan object. 12 00:01:21,590 --> 00:01:23,810 And for now, I'd like to remove the setas. 13 00:01:29,740 --> 00:01:32,080 Then we're going to print the fields from Nissan to. 14 00:02:01,700 --> 00:02:03,610 OK, nothing weird so far. 15 00:02:05,890 --> 00:02:09,250 Now, what if I decide to paint the second Nissan yellow? 16 00:02:24,310 --> 00:02:25,120 Run the code. 17 00:02:34,350 --> 00:02:39,600 Oh, I meant the only update, the second Nisan, but now the first Nissan is yellow as well. 18 00:02:40,810 --> 00:02:42,040 You fell into the trap. 19 00:02:47,470 --> 00:02:51,080 When you set a variable equal to another, it copies the value inside. 20 00:02:51,610 --> 00:02:57,730 In this case, what's inside is a reference and now both variables share a reference that points to 21 00:02:57,730 --> 00:02:58,720 the same object. 22 00:02:59,200 --> 00:03:02,650 If I update the object, the one variable, it's going to affect the other. 23 00:03:03,400 --> 00:03:04,810 This is bad. 24 00:03:08,780 --> 00:03:14,930 The state of a variable should not change because you updated another, the reference shop can make 25 00:03:14,930 --> 00:03:17,090 your code dangerous and unpredictable. 26 00:03:21,410 --> 00:03:25,730 The solution is to make a new object, don't be lazy is create a new object. 27 00:03:29,870 --> 00:03:34,550 Inside your code set the variable Nissan to equal to a new object. 28 00:03:37,260 --> 00:03:42,270 And after creating a new object called the constructor, by passing the four values that it needs to 29 00:03:42,270 --> 00:03:43,470 update your fields. 30 00:03:49,330 --> 00:03:50,590 All right, run your code. 31 00:03:54,500 --> 00:03:58,190 And as I expect, all of the objects are independent of each other. 32 00:04:01,860 --> 00:04:07,700 Instead of setting the variables equal to each other, we made a new object and now my mind is at peace. 33 00:04:07,980 --> 00:04:11,860 I can take comfort knowing that each variable points to a unique object. 34 00:04:12,270 --> 00:04:16,019 I don't have to worry about the state of one variable being affected by another. 35 00:04:21,720 --> 00:04:25,400 This is a great solution, so we can update our table of traps. 36 00:04:26,040 --> 00:04:28,110 Don't worry, it's all in your cheat sheet. 37 00:04:32,990 --> 00:04:38,750 So far, we went over to reference drops in this video, we talked about the second trap do not set 38 00:04:38,750 --> 00:04:40,520 object variables equal to each other. 39 00:04:40,880 --> 00:04:42,440 This doesn't copy the object. 40 00:04:42,440 --> 00:04:43,730 It copies the reference. 41 00:04:54,190 --> 00:04:57,040 Instead, set your variable equal to a new object. 42 00:05:00,550 --> 00:05:05,980 It's much better if a variable points to a unique object, then you don't have to worry about the state 43 00:05:05,980 --> 00:05:08,500 of one variable being affected by another.