‫Instructor: Hi.
‫Within this lecture,
‫we're going to discuss a term called scope.
‫So scope means range.
‫So why do we use that kind of a term?
‫Because we have different scopes over here.
‫Like we're creating new coding blocks with each method,
‫each class that we create, right?
‫So for example, over here,
‫we have defined an integer called X,
‫and its current value is eight.
‫So suppose that I want to change the value of this X.
‫Can I do that?
‫Of course I can, right?
‫I can just come over here and say, X is nine.
‫It will work,
‫because I haven't declared it final or anything,
‫it's not a constant,
‫it's a variable.
‫I can change its value.
‫So as you can see, value of X is nine.
‫But can I reach that X
‫from any other method like this?
‫Can I reach that X from here?
‫Like can I say X is 10?
‫Of course I cannot,
‫because it doesn't belong over here,
‫it doesn't belong in here.
‫So it's in a different scope.
‫If I do this,
‫and X is 11, or something like that,
‫then I can reach that X.
‫Nope.
‫I can create a new X.
‫I can create a new variable,
‫but they're not the same X,
‫they're not the same variable.
‫For example, I can write value of X in math plus X,
‫and run it.
‫And I will see the value of X will be nine,
‫but value of X in math will be 11.
‫And they're named X in both cases,
‫but they're completely different variables.
‫They are in different scopes.
‫So you cannot reach variable that you have defined
‫in one method or one scope
‫from another scope.
‫Okay?
‫So you have to pay attention to this range issue,
‫because sometimes you may want
‫to create the one variable
‫and reach that variable from all of the methods
‫or all of the class.
‫Then how do you do that?
‫Of course, you can use the class itself.
‫I'm under the main activity, okay?
‫But I'm not inside of any method right now.
‫Now I can create in any variable over here as well,
‫like string username, for example.
‫Suppose that I have a username string
‫and I want to reach that from every method,
‫because I will do something to that variable,
‫and I'm gonna do different things on different methods.
‫So right now, I have defined this username
‫and I'm initializing the username,
‫giving it a value under on create.
‫So whenever this activity is created,
‫username will be initialized with a value of James.
‫And I can do the same thing over here,
‫like username, Lars.
‫Okay?
‫Username is Kirk, for example.
‫And from the new methods,
‫maybe username something else.
‫Beware that I'm not using string username,
‫I'm not creating a new variable each time.
‫I'm just changing the value of the same variable,
‫same object that I'm referring over here,
‫like username, username, username.
‫And since I have defined this globally inside of my class,
‫I can reach it from anywhere I want, right?
‫So this is cool.
‫I have a variable that I can reach
‫from every method that I have.
‫And this is defining this as initializing.
‫Okay?
‫I can initialize this as an empty string, for example,
‫and within each method I can
‫give it another value, like this,
‫username is Lars.
‫If I call test method,
‫but username will be Kirk
‫if I call math methods.
‫Maybe you have different buttons
‫to assign different names to a username,
‫I don't know.
‫You can do it with this.
‫Okay?
‫So this is a very important concept.
‫First, we can not reach
‫if we declare a variable,
‫if we define and initialize a variable like this, okay?
‫In a method from anywhere else,
‫but we can if we define it under class.
‫So for example, right now,
‫this username is not the same username
‫with the other ones,
‫but if I just remove the string,
‫they will be equal to each other.
‫Okay?
‫So this is how scope works,
‫This is how Android Studio tries to understand
‫which variable is which.
‫So let's stop here
‫and continue within the next lecture.