Okay, so in the previous couple of videos,
we learned what Packages were and how to actually use them.
It's now time to actually have a look at Scope and
that looks like granting a restricting x's to our objects.
So the thing is with Scope it's probably one of the easiest things about
programming, once you understand it, but it's actually also one of
the most confusing Until you do actually understand it.
So scope here actually refers to the visibility of a class, member,
or variable.
So to consider that in non-competing terms, let's imagine you're going to buy
a particular computer, and you find a local store and
let's just say it's got the computer for 20% off, 20% discount.
Now, the same computer in another store would still be at the full price, though.
So the scope, in other words, or the price reduction is just that one local store.
But, contrast that to a situation where you've got a large company, and
let's use Wal Mart as an example.
Huge company in the United States,
and say if they decided to discount a particular computer, so
the discount then would actually apply in about 4,500 stores across America.
So in that context the scope of the discount is therefore much much wider.
And that actually asks what made us a multi national company.
If they applied the discount world wide, the scope would be over 11,000 stores.
So the important thing here is that Walmart's discount does not apply to any
other stores.
So the scope is limited to the stores controlled only by Walmart.
And the same with the local example, the local store that had the 20%.
The scope there is just for that one local store.
So the same computer and in a Java context would be the same variable.
In other stores, it's not discounted.
I hope that makes sense.
Our Java objects have got scope in much the same way.
So what we're going to do now is code up an example To discuss the scope.
I'll show you the scope of various objects to help you
sort of understand scope a little bit better.
So we're going to start off by creating a new class and
we're just going to call that ScopeCheck.
So I'm just going to click on a new class, ScopeCheck is the name of it.
And what your going to do is create a few variables for this one.
Remember variable public, it, of course when public equals zero.
Probably also have a private private file equals one.
We'll add a constructor.
This constructor won't have any parameters.
But what we'll do is,
we'll actually just print out a message to say that, scope check created.
And just show the variable values.
So public var equals + publicVar + and
": privateVar = " + privateVar,
just to show the values.
And we'll also create a getter as well, we'll need that shortly.
And the getter will be just for the privateVar.
Like so, so there's our basic scope check.
So what we're gonna do now is go to the Main class and to the main method, and
check and create a new instance, and then check the value of privateVar in main.
So to do that, gonna go back to main.
And here in the main method, we're gonna do String privateVar.
Actually, the name is the name as typed into the Scopecheck.
this is private to main().
Then we'll create an instance of Scopecheck.
So Scopecheck Scope instance,
doesn't matter what we call it, equals new, scope check.
And we'll just talk some apple, the first one we'll put is scope instance.
Scope instance privateVar is
scopeInstance.getPrivateVar, using the getter.
And then we also want to print out our value from privateVar.
And of course we're noticing that the privateVar there and privateVar
here are the same named variables, but they're actually separate variables.
So we've got two variables called private file.
One's the string which I'll have to find
above in the main method you'll see on the screen.
And the other is the EDNS scope check object that we created.
It's just going to be scope instance.
And we can only access the private file.
The scope [INAUDIBLE] getter.
Again the getter was what we created here.
We can't access that directly.
So we will actually run this.
And surprisingly we get the first one.
Skype instance prog file as one and
that was called form the constructor through a call.
We put that message in there.
And this is private to main.
It's the same variable name, but
a different variable that is local to the main method.
And that's got a different value and
is actually a different type in this case it's actually a string.
So within the main method itself, whatever we refer to privateVar,
Java actually knows that must be referring to the one declared with main,
as that's the only one that's available in the current scope.
So, in that, in other words, it's referred to Variables that refer to in scope.
So access modifiers such as public, private, and protected.
There's actually one way to restrict the scope of an object and
looking at these access modifiers in more detail shortly.
But as it turns out, scope is even more important within the same class or method.
So what we're gonna do now is shoot over to ScopeCheck, our ScopeCheck class, and
we're gonna create a new method there.
And we'll call that one public void timesTwo, and it's just
gonna be a simple method that actually prints out some of the two times tables.
So we're gonna start with a definition, privateVar Equals 2.
Note the name of the variable.
And note the fact that on line eight we've also got a variable of the same name.
For int i=0; i<10 i++.
And we're just going to output i
plus [SOUND] Times two is, plus i times [INAUDIBLE] bar.
Very very simple as you can see there.
So the method as you can see as I mentioned also declares a variable called
[INAUDIBLE] bar, and sets the initial value of two.
So when we use it in the loop,
i is multiplied by the value of this one, of this variable here.
And not this variable here.
And that's because privateVar that's actually local to this method
is actually in scope.
So just to confirm that actually is the case we're going to go back to
our main method.
And let's actually just print that out.
We'll just run this method so scopeInstance.timesTwo.
So if you run that you should actually confirm that that's working.
Zero times two is zero.
One times two is two.
Two times two is four.
And so on. So, it's clearly multiplying by two.
And if you go back and actually look at our code.
We've got this line here on line nineteen setting the local
variable private file to two.
And obviously if Java wasn't reading that, it'd be coming back up here.
And looking for private var which is the of the value one.
So, you can actually save that in our example it clearly did work in model pod
[INAUDIBLE] two.
So, by declaring that private var again inside the times two method
we created the new variable who's scope is the current method.
So, any time we refer to private var inside the two times method But
now actually referring to the variable within the most local scope.
So, there they got the same name.
The rules of scope ensure that the variable with the narrow scope
is the one that is used.
Narrow being the one that is most local.
So, in this case private file.
In the times 2 method.
So another way to consider this to use the concept of enclosing blocks.
So when Jarvis is a reference to a variable, what it does,
it starts by checking the current block of code to
see if the variable was actually declared there.
Now if it is declared there, then that variable is used.
But if there's no such declaration and it's not there, then Java actually checks
any block that encloses the current one to see if there's a declaration there.
So it keeps doing this working backwards until it finds the variable declaration.
And if it doesn't actually find one, then you'll actually get an error in your code.
And, of course, the code won't be able to run because the variable won't be found.
So in this times two example,
there's no declaration of private within the current block.
The current block being the fall of I'm talking about.
There's no declaration in there so
Java actually takes the enclosing block which is the method itself.
So this would be the current block The block in braces for the four loop and
obviously where the brace starts and ends that sort of that group of code,
there's no private variable to find, there's no variable without name define so
then take it out to the next level which is the next enclosing block which in this
case is the entire method and it does actually find the variable private
file and that's why it's able to use it because that's the next most local one.
Now there's two variables as you can see within this four loop that
are actually being used.
We've got private var as we've discussed just now and also I, so I's actually
declared in the four loop so here in this loop here is actually defined,
it's actually in that loop, so it's in that scope the scope of the two braces in
other words, so the scope is limited to that block
plus any contained block if there were any in which there isn't in this case.
So to check that, we can actually try doing this and we should get an error.
So if we try doing something like
value of i is now plus i we're immediately gonna get an error.
As you can see, it's in red.
That intelligent actually showing there's an error because it cannot result in and
if we actually tried to run it with actually getting the same error as well.
Because ours was declared in the full loop but a scope is actually restricted to that
in that loop and it's not actually available outside that loop.
So as soon as the loop terminates.
In other words, as soon as it actually finishes off 9 times 2 is 18.
So once it's done that, and it's exited that block of code effectively that's it.
The variable I then goes out of the scope.
So just to confirm again with the scope.
So what we'll do now is we'll comment this line out because we'll not need it because
it's not going to work.
If I was to comment this line out here, the private variables two,
which is the local variable we introduced with the tones two method,
obviously we haven't got an error.
The reason now is, what's happening is Gyro's looked at that and
determined there's no local variable and kept looking above,
sort of expanding its search, and it's found this private bar in script check as
a number variable, so if we run this now Even though now it says times two.
That's because we eventually top that two.
But you can see the calculations clearly are now multiplying by one.
So it's actually using the value of
amid the variable on line 8 instead of the local variable that we defined on line 19.
We'll just that again, so it's actually working.
And obviously if you run it again We now get the correct answers again.
Now note here in this situation that the class field
with the value one still exists, and it retains it value.
And it can actually be used outside of the two times method.
And as it turns out we can actually even use it within in the two times method
even if we've also got a variable of the same name.
And what we have to do in that scenario is we need to tell Java that we wanna use
that privateVar rather than the local one.
So what we do is we do that by qualifying the variable name with this.
So in other words, if we did it specifically you had this variable on
line 19 It was too, but you specifically wanted to use this variable that we
defined at class level and we can do that we can change this and
we just actually type the word disk dot in front of private file and
that actually tells java Use this variable on line eight,
even if you've got a variable of the same name, in scope effectively.
And you've seen that in the course a lot of times with getters and
setters, when we refer to this type of field then to actually specify the field.
And obviously if you run that, we're back to multiplying by one again,
and that's because we use this dot and
Java knows then what we're actually talking about This variable on line 8.
And incidentally, if I commented that out, we didn't get an error,
this.privateVar doesn't exist anymore, because as far as Java's concerned,
you've told it to look for a field in this class by the name of privateVar,
and there's no longer one because we've commented it out.
So I'll just undo that again.
And I'll just set this back to where it was as well.
Now the concept of scope doesn't just apply to variables.
Methods also have scope, as do classes.
Class scope can be a little bit confusing, but
let's actually start by adding an inner class to our ScopeCheck class and
have a look at what it does to the scope of things.
So I'm gonna come down here to bottom of ScopeCheck and add an inner class.
So I'm gonna call that public class InnerClass {.
Let's set up a public variable, public int private var.
And sum it the value of three, so it's distinct [INAUDIBLE] else.
We'll create a constructor and we'll just do a simple output
in the class Created
privateVar or is privateVar.
And let's also add exactly the same method we had there.
The times two method and i'm going to paste that in here.
Again, that's a method for inner class.
And I'll just delete that.
[SOUND] It's a little bit confusing with that, now I'll comment that out, so
I'm gonna delete that there as well.
Making sure that I haven't got this specified anymore in either of
the [INAUDIBLE] methods.
And what we want to do then is create an instance, of this inner class, and
we'll go back to do that in a minute.
When you do that using that slightly odd [INAUDIBLE] text we covered previously,
and with previous videos, within the classes [INAUDIBLE] scopecheck.innerclass.
InnerClass equals scopeInstance.new InnerClass.
Plus on notes with instant text but we have talked about that in previous videos
and let's now run that code innerClass .timesTwo.
And if we run that.
What's we'll do here is we'll actually put some formation.
Some asterisks here just to make it clear.
And we actually run this.
So the inner class was created and we've got the same calculations happening.
So, that's all working fine.
Once again, getting back to the code.
The rule of scope applies and
because private declared this a field of the inner class, this has we're talking.
And actually what i'll do make this clear i'll actually delete this
declaration here.
That's what you want it to do.
So, in the times two method for you in the class i'm actually deleting the run that.
This is what I wanted to show you.
So in this case, what's happening well previously when I had the local
variable in there, you used that.
But soon as I've actually deleted that, of course the scope,
the Java's going back and checking what the scope is, and it comes back and
finds [INAUDIBLE] which is defined here, in the inner class as a field.
And it's using the 3 for
calculations as you can see there, 3, 6, 9, 12, 15, et cetera.
So in this case with the inner class, this definition here public in, private by
equals 3, overrides the prominent private r which is in our scope check r And just
out of interest it doesn't actually shadow the privateVar in ScopeCheck times two.
So on this first on here at all.
Those two blocks are not enclosed by or enclosing each other so there's no actual
possibility of either variable being in scope in the other method.
And once again, we can actually comment this out if we wanted to here.
We don't get an error as you can see there.
And if we run it again We're getting the calculations multiplied by 1,
so what's happening again is, it's looking for privateVar, cannot find it in
the inner class or couldn't find it as a local variable in the timesTwo method,
so it's come up here to privateVar in ScopeCheck and retrieved the value of 1,
and our calculations actually use that again.
Now we actually came back here to time two within the inner class and we use this, we
actually get an error and that's because it's looking for the field in inner class.
Now obviously that hasn't got a value because it hasn't been defined with
an inner class so what we can do is example, we can type scope check dot this.
.privateVar, which
confirms that this is the variable up here that we're actually talking about.
You could run that and
you can see that's calculating correctly based on the value of 1.
And if we actually uncomment this out again.
So we now have got a field in the inner class called privateVar.
If we run this We're still getting the same calculations based on
one multiplying by one because that variable's been defined in scope check and
because we've used the name of the class.this.
I would have explicitly said Use this field in the scope check class.
And just out of interest, in the times two method we could use scope as well.
We could actually if we wanted to actually call the times two method.
We could do scope check dot this dot times two if you wanted to.
To specifically call that method that is a method in the scope check class itself.
So, what i'm gonna do is actually finish this video here and
now it's gonna be a bit long.
In the next video we're just going to finish off a few minor things to do with
scope, and then we're going to talk about visibility.
And then we're gonna start on a challenge.
So I'll see you in the next video.