Now, you should have determined that message is a global variable.
And you can tell this from looking at where it's declared, which is up here.
It is not declared in any of these methods.
It's declared, in fact, outside of any methods.
It is, though, declared between the curly braces
of what we've been calling the main activity class.
Okay. So the next question is,
should message be a global variable.
Well, whenever we use message,we see that we set what message is,
and then we immediately display it.
When we set what message is, we don't look at a previous value of message.
Global variables are good for keeping track of a previous value between
things like button clicks or other events you might have in your app.
They're a way to save the state of an app.
What they're not good for
is when you're using a variable as a sort of intermediate value.
Remember, when you're executing the code inside one of these
blocks called a method, you do each statement sequentially.
If your variable is only needed within the sequence of those statements then it
doesn't need to be a global variable.
It's being used locally.
It's being used just in the method.
And that's the case with message.
It's a kind of intermediate value used just at that moment.
So, message should not really be a global variable.
Now, will the program not work because it's a global variable?
No, it'll work just fine, but let's say I forgot to type this line here.
Okay, so if I click the No button.
What it's going to do is, well first it's going to take healthLevel and
subtract one from it.
Now as a side note you might be wondering what the value of
healthLevel is.
And well, we'll talk about that in the next video.
But for now let's just assume it's zero.
So we take zero, we subtract one.
Their healthLevel is at negative one.
And well, we don't have this line anymore, so
we just print out whatever message is.
Well, what is message?
When a global variable is created and
it isn't given an initial value, it's given a default initial value.
For strings, the default initial value is the empty string,
which is basically is a string that has no text in it.
This would mean that the user doesn't see anything when the no message
clicked.
That's not great, but not entirely unexpected behavior.
When things get really kind of funky,
is let's say that you hit the Yes button first.
This will make it, you answered yes, current healthLevel is one, and
show it on the screen here.
Let's say then after that, you hit the No button.
It'll subtract one for the health level making the health level zero again.
But it won't set message to another value because let's say we
deleted this line.
So message will have the old value from up here.
You answered yes, current health level is one.
And it will display that again.
Again, this all happens because healthLevel now exists between
multiple methods.
So whatever value was here before, if we don't change it here,
we'll still be there.
And no sort of compile error will be thrown.
Now if message was a local variable, when you forget to set it to anything
here, it will complain and actually not let you run your app.
But because you've made it a global variable, it won't complain.
It will just print out what it had before from the previous method.
Which is wrong.
So to summarize, message should not be a global variable.
Why?
Well it's not really used in between methods.
Or it'll have some value from another time that
the variable was used in another method.
Because a global variable will have a default value set, or
it will have an old value from when it was used in one of these other methods.
Now it's only if we make an error, and we forget to set it to the correct
thing, but we shouldn't make our job any harder for ourselves.
Okay, that's only one of the variables in this example, so
let's look at the other one, healthLevel.