11.4 Scope2

Cliquez içi pour plus de vidéos disponibles sur notre chaine youtube !

So what we’re going to do now is go to our ScopeCheck class and we’re going to create a new method so it’s going to be public void foisDeux. And it will be just a simple method that will display table of two multiplication. So let’s start by creating a privateVar variable that is equal to two and note the fact, that we have the same variable name. So, after we’ll do a loop for And inside this loop, we’ll just display i x private var. very simple as you can see in loop i is multiplied by the value of that variable here. And not that variable here. And that’s because privateVar is actually local to this method, it’s just in this method. So, just to confirm that this is really the case we will go back to our main method. And We will simply execute this method so, it will be ScopeCheck So if we relaunch our program, we see that they are clearly multiplied by two and if we return to our ScopeCheck class we can see that it executes this one and not this one. So by declaring privateVar again in the multiple method We have created a new scope variable So, every time we talk about privateVa within the multiple methodThey we refer to the variable in the most local context. So, although they have the same name. The scope rules ensure that the variable with the narrow scope is the one used. That was it for this video I think it was a bit complicated to understand the scope of the variables. And as soon as you understand, everything is easy. Finally almost everything, I’ll see you next!