Oh, boy, the reference trap.
This is my favorite lesson because I've seen the best developers get caught in the trap.
That's because nobody told them about it and they had to learn the hard way by falling into the trap.
But because you're my student, I'm going to make sure that you don't fall into any traps.
I told you earlier that a variable cannot store an array, it stores a reference that points to it because
of this, another variable can actually store reference that points to the same array.
Isn't that weird?
As far as I know, there isn't an official name to describe this phenomenon.
So I call it the reference trap.
In this lesson, we're going to talk about the reference drop, the violation that causes it and the
solution that prevents it.
First things first, inside Section six, make a new file named reference, drop Java and inside the
class, make sure it has a main method.
This trap happens with many variables, Cheryl, reference to one array.
This is about and I call it the reference strap, because if I change the area through one variable,
the other variable gets affected as well.
In this example, I set numbers equal to an array of three values.
But notice that I can update the array from another variable numbers to.
This is bad.
This is a trap, and to avoid the reference trap, do not set array variables equal to each other.
That's because, well, suppose you have a variable that stores an array of integers, numbers, stores,
a reference that points to the array.
If I set another variable equal to numbers, it's going to copy what's inside and what's inside is the
reference.
So now both variables share a reference to the same array.
And so the reference trap happens when we set array of variables equal to each other.
No stores a reference that points to the array and by setting numbers to equal to numbers, it copies
the value inside and what's inside is the reference.
Now, both variables share a reference to the same array.
So it's not a coincidence that number two is reference is also a pink circle.
I want to commit this violation and show you the trappin code.
So let's assume you're a manager at the Java Cafe and you have the staff list from last year.
I'm going to set a variable string staff last year.
Is equal to an array of three values, Tommy.
Joel and Ellie.
And now I'm going to print the contents of staff last year as one long string.
Good.
Nothing too hard, we're just printing an array of string values, and now this year, only one staff
member changed and you're too lazy to rewrite another array.
So you decide to set another array variable equal to this one string stuff this year is equal to staff
last year.
And you're going to print the contents of the story as well.
OK, we're off to a good start.
And now, unfortunately, Joel went missing in a new employee named Abby took his place.
The staff this year changed.
So we're going to change the next one from the second variable.
We were on the code.
Oh, that's not good.
I meant the only update this year staff list, but last year, staff list updated as well.
This is very bad because now last year's data is ruined.
This is the reference trap I was telling you about.
When you set a variable equal to another eight copies, the value inside, which is a reference.
If I update this right through one variable, it will affect the other because now they both point to
the same array.
This is bad.
The state of a variable should not change because you updated another.
In a perfect world, every variable should point to its own array.
So what's the solution if I want to copy an array into another variable, how do I do it?
The solution is to make a new era.
This trap occurs when you set array variables equal to each other, so don't be lazy, create a new
array and then copy every evalu into the new array using a for loop.
So back in our code, we're going to do just that, I'm going to set stuff this year equal to a new
string array with a length of three elements.
Then I'm going to create for a loop that starts at ENTI equal to zero.
And it's going to run through every element in one of the arrays, they both have a length of three,
so it doesn't matter which length you use.
Plus.
And during each run, I'm going to update each element and stuff this year by setting it equal to an
element from staff last year with the same index.
Now I can rerun the code.
Okay, perfect.
We successfully copied every element from the staff last year to the staff this year.
Now what happens if we change the next one from the second variable?
So I'm going to change the next one, I'm going to set that equal to Abby.
And the world makes sense again, sweet.
That's because you stuff that you're equal to a brand new array.
The loot copies every value from staff last year to the same index stuff this year, and now each variable
stores a unique reference to its own array.
Updating the second variable should have no effect on the first.
Now, here's a better solution, erase that copy of because for something so simple, the for loop is
so much code.
The erased utility class that we've been using to call the two string method has another useful method
called copy of.
It takes two arguments the area that you want to copy and how much of it you want to copy.
This looks a lot easier and it seems like a much better solution.
Someone to a better table.
And don't worry, this will be in your cheat sheet.
So in your code, you remove this monstrosity.
And replace it with a raised copy of.
And erase that copy of requires two arguments, the you want to copy stuff last year and how much of
it you want to copy.
We're going to copy the full length of the array.
All right, and we get the same result.
That's because stuff this year is equal to a copy of the old array, I left out the second argument
in this animation just because there wasn't any room.
But you get the point.
Each variable points to a unique array.
I hope now you realize that working with a raise is like playing with knives, but as long as you're
careful and you follow my table, you should be fine.
In this video, you learned about the reference drop, the reference trap happened when you mishandle
the race, do not separate variables equal to each other.
This doesn't copy the array.
It copies the reference.
The right way to copy arrays is to use erase that copy of it sets the variable equal to a copy of the
original array.
And now each variable points to a unique array.
And you don't have to worry about the state of one variable changing because you updated another.