Advertisement
English transcript
Okay Katherine.
>> Mm-hm. >> We hit the plus sign.
[SOUND] And the number of cups of coffee goes up from two to three.
>> Okay. >> But we keep hitting the plus sign.
[SOUND] Again.
[SOUND] And again.
[SOUND] And the number of cups of coffee just stays at two, it's not going up.
>> Okay, well I think it's because we're missing one concept, and
that is variable scope.
>> Variable scope?
>> Yeah.
>> Care to explain?
>> Variable scope means that when a variable is declared in a method,
it can be only used in the scope of that method.
>> [SOUND] >> Pay attention.
>> Katherine.
>> Yeah. >> We're talking in gibberish.
Someone needs to give students in this class a medal for
getting this far ahead.
>> Okay. >> Let's slow down and explain clearly.
>> Sorry. Okay, I can explain.
>> Okay, so we hit the plus button.
>> Mm-hm. >> And the increment method inside
a code gets called.
>> Yes. >> And then what happens?
>> Well, we create the quantity variable.
>> Right.
>> It is initialized to value two.
>> Right. >> And we
update it to three cups of coffee.
>> And then when the increment method ends, then what happens?
>> Well, then the variable gets destroyed.
[SOUND] >> Okay, so
when I press the plus button again [SOUND] the increment method gets called
in our code- >> And
then we create a quantity variable.
>> Oh, wow.
Another one magically appeared.
>> Yeah, a brand new one.
>> Okay. >> And it's initialized to two, and
then we update it to three.
>> And then the increment method ends again, and then what happens?
>> The variable gets destroyed.
[SOUND] What we really need is a global variable.
>> Oh, yeah.
A variable that can be accessed by any, and all, pieces of code inside our app.
>> Exactly, like any method, the increment method or
the decrement method.
>> Mm-hm. >> So we create
a global variable like this.
It's still the quantity variable.
>> Mm-hm. >> But now it's global.
>> Mm-hm. >> So after a method finishes executing,
it doesn't get destroyed.
>> Okay, let's use this in our code next.
>> The problem in our app is that if I hit the + button a bunch of times,
the number of cups of coffee never increases beyond 3.
The reason is because the + button triggers the increment method.
In this method, we always create a new quantity variable, and
initialize it to 2.
So when we update the quantity, it's always set to 3.
This method has no memory of the fact that I already called it before
by clicking on the + button, and that I'm already displaying 3 cups of coffee.
It has no idea that I want 4 cups now.
In the old code, we declared a local variable for quantity in the increment
method and a local variable for quantity in the decrement method.
The reason why we can declare a variable with the same name twice is
because they are in different variable scopes.
This is the scope of the quantity variable declared in
the increment method.
Within these bounds we can refer to the quantity and update the quantity.
But outside of these bounds,
other methods would not know about this quantity variable.
You would get an unresolved symbol error because
other methods wouldn't know about this variable.
Similarly, this is the variable scope for
the local quantity variable declared in the decrement method.
Within this method, you can display the variable and update the variable, but
beyond the bounds of this scope, you can't reference it.
In the updated code with a global variable,
this is what it would look like.
Instead of declaring the quantity variable in the increment method,
we've pulled it out to be declared at the top of the main activity.
It's declared within the opening and closing braces of the main activity.
So anything inside the main activity will know about the quantity variable.
This is the variable scope for this global quantity variable.
All the methods and other variables in the main activity
can now refer to the quantity, and also update it.
So the increment method updates the quantity variable, and also displays it.
And the decrement method also updates the quantity and displays it, and
the quantity always refers back to the global variable here.
The advantage of pulling it out into a single global variable is now we have
a single source of truth for how many cups of coffee the user wants.
Even if we've increased the number of coffees a bunch of times,
then if we decrease the number of coffees,
then it'll just use the last known value of the quantity.
Before it was weird that we had two quantity variables, because those
two variables could get out of sync, and the user ends up only seeing one value.
So it makes sense to have one single source of truth,
a global variable to keep track of the number of coffees ordered.
You might wonder why we don't just make everything be a global variable and
have it solve all our problems.
You really don't want to make a variable global unless there's a real need
that other things in the main activity need to reference that variable for.
If you only need to use a method within the context of a single method,
you should just keep it a local variable.
It's sort of like having a secret.
You only tell people on a need to know basis.
Similarly, you only want to reveal variables
that other people absolutely must know about in order for the app to work.
In the MainActivity in the increment method,
I'm going to copy this line of code that declares the quantity variable.
I just hit Cmd+X, or it's Ctrl+X to cut.
And then at the top of the main activity class, I'm going to paste it.
This will be pasted right after the opening breaks of the main activity.
Notice that it is outside the braces of other methods.
Otherwise, if it was inside the braces of a method,
it would be considered a local variable.
Quantity is now a global variable within the MainActivity class.
We'll learn more about classes in the next lesson.
The MainActivity is a class that contains methods and variables.
Now, any method in the MainActivity can refer to this quantity variable.
So this increment method just updates the quantity and displays it.
Notice that a global variable has a purple font color for the variable name.
If it's a local variable, the font color is black for the variable name.
So that's how you can tell the difference between local and
global variables.
Speaking of local variables, we want to get rid of this local variable in
the decrement method, because we want to use the global quantity variable.
The variable name in these methods must match the global variable name, exactly.
If I said quantities here, it would give an error because
quantities is not defined as a local or a global variable anywhere.
And I'll undo that so that it works again.
If I run the app, let's see what happens.
The app starts off at a quantity of 2.
If I hit the + button, it actually increases.
If I hit the- button, it actually decreases.
That's pretty awesome.
We've succeeded in building our quantity picker.
Now, I want you to be able to build this on your phone so you can increase it or
decrease it as much as you want to your heart's desire.
We've included a link to the MainActivity code for you to reference.
I'd rather have you type it out yourself by following this video
instead of copying and pasting the code.
That way, you'll be able to create global variables on your own
in the future.
I'm going to attach the debugger so
we can see carefully what's happening with the quantity variable.
I'm going to add breakpoints in the increment and decrement methods and
then run the debugger.
When I hit the + button, then this breakpoint triggers.
Before this line of code has even executed,
we see that the global quantity variable already exists with a value
of 2 because of this line of code up here.
If I set to the next line, then this line has finished executing and
we've increased the quantity up to 3.
I can hit resume so that the app updates to a quantity of 3.
If I hit the + button again, then before I've even executed this line of code,
we already have the global quantity variable set to a value of 3.
That was the last known quantity we had before, and it sill remembers it.
If i increase the quantity by 1,
then the global quantity variable has a variable of 4 now.
I can hit resume to update the screen with 4.
Now, we can see that every time the increment method is called ,we can
actually continue to build off of the existing quantity value.
Let's see how it interacts with the decrement method.
If I hit the- button, then this breakpoint triggers.
The global quantity variable has a value of 4,
which is what we last left off with.
If I decrease the quantity by 1 than it goes down to 3 cups of coffee.
And I can display that on the screen and it changes to 3.
With a single global variable for quantity, the increment and
decrement methods can now increase or
decrease the number of coffees by 1 based on the latest value of quantity.
Now, it's your turn, go ahead and
replace the 2 local variables with 1 global variable.
Then, use a debugger to verify that the quantity variable is actually
updating as expected.
Once your quantity picker works, go ahead and do a little happy dance.
And then you can try to experiment a little bit.
What happens if you have a different initial quantity value, like 0?
Or try doubling the quantity each time the + button is pressed.
Or halving the quantity each time the- button is pressed.
Feel free to experiment with whatever you're curious about.
If you ever wondered, what would happen if I do this or that?
Those are sometimes some of the best learning moments,
because they're unexpected.
When you're done check these boxes to continue.
>> Mm-hm. >> We hit the plus sign.
[SOUND] And the number of cups of coffee goes up from two to three.
>> Okay. >> But we keep hitting the plus sign.
[SOUND] Again.
[SOUND] And again.
[SOUND] And the number of cups of coffee just stays at two, it's not going up.
>> Okay, well I think it's because we're missing one concept, and
that is variable scope.
>> Variable scope?
>> Yeah.
>> Care to explain?
>> Variable scope means that when a variable is declared in a method,
it can be only used in the scope of that method.
>> [SOUND] >> Pay attention.
>> Katherine.
>> Yeah. >> We're talking in gibberish.
Someone needs to give students in this class a medal for
getting this far ahead.
>> Okay. >> Let's slow down and explain clearly.
>> Sorry. Okay, I can explain.
>> Okay, so we hit the plus button.
>> Mm-hm. >> And the increment method inside
a code gets called.
>> Yes. >> And then what happens?
>> Well, we create the quantity variable.
>> Right.
>> It is initialized to value two.
>> Right. >> And we
update it to three cups of coffee.
>> And then when the increment method ends, then what happens?
>> Well, then the variable gets destroyed.
[SOUND] >> Okay, so
when I press the plus button again [SOUND] the increment method gets called
in our code- >> And
then we create a quantity variable.
>> Oh, wow.
Another one magically appeared.
>> Yeah, a brand new one.
>> Okay. >> And it's initialized to two, and
then we update it to three.
>> And then the increment method ends again, and then what happens?
>> The variable gets destroyed.
[SOUND] What we really need is a global variable.
>> Oh, yeah.
A variable that can be accessed by any, and all, pieces of code inside our app.
>> Exactly, like any method, the increment method or
the decrement method.
>> Mm-hm. >> So we create
a global variable like this.
It's still the quantity variable.
>> Mm-hm. >> But now it's global.
>> Mm-hm. >> So after a method finishes executing,
it doesn't get destroyed.
>> Okay, let's use this in our code next.
>> The problem in our app is that if I hit the + button a bunch of times,
the number of cups of coffee never increases beyond 3.
The reason is because the + button triggers the increment method.
In this method, we always create a new quantity variable, and
initialize it to 2.
So when we update the quantity, it's always set to 3.
This method has no memory of the fact that I already called it before
by clicking on the + button, and that I'm already displaying 3 cups of coffee.
It has no idea that I want 4 cups now.
In the old code, we declared a local variable for quantity in the increment
method and a local variable for quantity in the decrement method.
The reason why we can declare a variable with the same name twice is
because they are in different variable scopes.
This is the scope of the quantity variable declared in
the increment method.
Within these bounds we can refer to the quantity and update the quantity.
But outside of these bounds,
other methods would not know about this quantity variable.
You would get an unresolved symbol error because
other methods wouldn't know about this variable.
Similarly, this is the variable scope for
the local quantity variable declared in the decrement method.
Within this method, you can display the variable and update the variable, but
beyond the bounds of this scope, you can't reference it.
In the updated code with a global variable,
this is what it would look like.
Instead of declaring the quantity variable in the increment method,
we've pulled it out to be declared at the top of the main activity.
It's declared within the opening and closing braces of the main activity.
So anything inside the main activity will know about the quantity variable.
This is the variable scope for this global quantity variable.
All the methods and other variables in the main activity
can now refer to the quantity, and also update it.
So the increment method updates the quantity variable, and also displays it.
And the decrement method also updates the quantity and displays it, and
the quantity always refers back to the global variable here.
The advantage of pulling it out into a single global variable is now we have
a single source of truth for how many cups of coffee the user wants.
Even if we've increased the number of coffees a bunch of times,
then if we decrease the number of coffees,
then it'll just use the last known value of the quantity.
Before it was weird that we had two quantity variables, because those
two variables could get out of sync, and the user ends up only seeing one value.
So it makes sense to have one single source of truth,
a global variable to keep track of the number of coffees ordered.
You might wonder why we don't just make everything be a global variable and
have it solve all our problems.
You really don't want to make a variable global unless there's a real need
that other things in the main activity need to reference that variable for.
If you only need to use a method within the context of a single method,
you should just keep it a local variable.
It's sort of like having a secret.
You only tell people on a need to know basis.
Similarly, you only want to reveal variables
that other people absolutely must know about in order for the app to work.
In the MainActivity in the increment method,
I'm going to copy this line of code that declares the quantity variable.
I just hit Cmd+X, or it's Ctrl+X to cut.
And then at the top of the main activity class, I'm going to paste it.
This will be pasted right after the opening breaks of the main activity.
Notice that it is outside the braces of other methods.
Otherwise, if it was inside the braces of a method,
it would be considered a local variable.
Quantity is now a global variable within the MainActivity class.
We'll learn more about classes in the next lesson.
The MainActivity is a class that contains methods and variables.
Now, any method in the MainActivity can refer to this quantity variable.
So this increment method just updates the quantity and displays it.
Notice that a global variable has a purple font color for the variable name.
If it's a local variable, the font color is black for the variable name.
So that's how you can tell the difference between local and
global variables.
Speaking of local variables, we want to get rid of this local variable in
the decrement method, because we want to use the global quantity variable.
The variable name in these methods must match the global variable name, exactly.
If I said quantities here, it would give an error because
quantities is not defined as a local or a global variable anywhere.
And I'll undo that so that it works again.
If I run the app, let's see what happens.
The app starts off at a quantity of 2.
If I hit the + button, it actually increases.
If I hit the- button, it actually decreases.
That's pretty awesome.
We've succeeded in building our quantity picker.
Now, I want you to be able to build this on your phone so you can increase it or
decrease it as much as you want to your heart's desire.
We've included a link to the MainActivity code for you to reference.
I'd rather have you type it out yourself by following this video
instead of copying and pasting the code.
That way, you'll be able to create global variables on your own
in the future.
I'm going to attach the debugger so
we can see carefully what's happening with the quantity variable.
I'm going to add breakpoints in the increment and decrement methods and
then run the debugger.
When I hit the + button, then this breakpoint triggers.
Before this line of code has even executed,
we see that the global quantity variable already exists with a value
of 2 because of this line of code up here.
If I set to the next line, then this line has finished executing and
we've increased the quantity up to 3.
I can hit resume so that the app updates to a quantity of 3.
If I hit the + button again, then before I've even executed this line of code,
we already have the global quantity variable set to a value of 3.
That was the last known quantity we had before, and it sill remembers it.
If i increase the quantity by 1,
then the global quantity variable has a variable of 4 now.
I can hit resume to update the screen with 4.
Now, we can see that every time the increment method is called ,we can
actually continue to build off of the existing quantity value.
Let's see how it interacts with the decrement method.
If I hit the- button, then this breakpoint triggers.
The global quantity variable has a value of 4,
which is what we last left off with.
If I decrease the quantity by 1 than it goes down to 3 cups of coffee.
And I can display that on the screen and it changes to 3.
With a single global variable for quantity, the increment and
decrement methods can now increase or
decrease the number of coffees by 1 based on the latest value of quantity.
Now, it's your turn, go ahead and
replace the 2 local variables with 1 global variable.
Then, use a debugger to verify that the quantity variable is actually
updating as expected.
Once your quantity picker works, go ahead and do a little happy dance.
And then you can try to experiment a little bit.
What happens if you have a different initial quantity value, like 0?
Or try doubling the quantity each time the + button is pressed.
Or halving the quantity each time the- button is pressed.
Feel free to experiment with whatever you're curious about.
If you ever wondered, what would happen if I do this or that?
Those are sometimes some of the best learning moments,
because they're unexpected.
When you're done check these boxes to continue.