Not it's just because a very important topic called variable scope every variable has a scope which 2
is basically the part of the program where the variable can be used and you get a compiler error if 3
you try to use it outside off at school different kinds of variables have different scopes. 4
That is variable is declared a class level have a different scope compared to variables declared in 5
methods. 6
Let's look at scope of class level variables variables declared at class level like instance are static 7
where tables are considered global and they are accessible anywhere in the class only exception is that 8
a class level variable cannot be assigned to another variable declared before. 9
And here is an example. 10
Here we are declaring three class level variables. 11
I janky these variables are accessible anywhere in the class so as you can see they are also accessible 12
in the method for you can also see that variable is declared. 13
And in the very statement we're assigning it to variable g so that it's perfectly valid. 14
But we also said that a class level variable cannot be assigned to another variable declared before 15
it so here you can see that we are declaring variable k but we are assigning it to variable in the previous 16
statement. 17
So this is invalid as gay is not declared when we are doing this assignment. 18
Background is that the same statement J called ok is valid within the method for. 19
Even though we're able K is declared below method for that because by that method fools and broke all 20
the class global variables are already initialized so it just doesn't matter. 21
Vary your methods are written within the class so that's a large scope of class global variables. 22
Next lips look at scope of local variables. 23
We know that local variables are declared within methods or constructors are within block statements 24
like statements a lookup table is accessible from the point of declaration. 25
And if it's block hit block and be method constructor can go a statement here's an example. 26
Here we have a method called fu the metalworkers for local variables b x z n y be them the metal parameter 27
on your scope is the and dark matter. 28
This scope of the x and z is also shown not the scope of media will X also includes the declarator site 29
that is a part that declares variable z. 30
That's the reason we're able to assign extasy but not that Z cannot be assigned to x when declaring 31
X that is you cannot how N x is equal to Z commas equal to something that will not work CSCOPE would 32
include any declaratives on the right plus the rest of the block next video right is part of an IF block 33
. 34
Unsoiled scope and so with that particular block. 35
So as you can see accessing variable right outside of the if statement is invalid and it leads to compiler 36
error and the other thing to note is that within the scope of a local variable you cannot really clear 37
that same variable. 38
For example you can see that a variable x is declared twice and the second declaration falls within 39
the scope of Fosdike tradition and hence is invalid. 40
Here is another example illustrating that within the scope of a local variable but you cannot really 41
clear that same area. 42
But here we are really getting variable x in the initialization part of the for statement and it is 43
invalid in the earlier example really coloration was done in an IF statement. 44
So what we just saw was that in local variable cannot be shadowed or hidden but we know that a local 45
variable can hide a class level variable. 46
We have seen that in the previous chapter so even though the class level variable scope is still valid 47
. 48
It can be done by a local variable. 49
And here is an example. 50
He had the class level variable x is hidden by the local variable x which is defined in the motard for 51
. 52
So when X is incremented it is basically incrementing the local variable x to access the class level 53
variable x. 54
We know that we need to use this reference Finally any mentors invoked from the current block will get 55
a new scope that is very declared in the current block are not accessible in the invoke methods. 56
Here is an example here and we have a method foo with a local variable x method isn't working method 57
bar Witan method bar b cannot access the variable X declared in method foo so that's about it underscores 58
the scope of both plus local variables and local variables I would say this just try out some of the 59
examples myself for book Class level and local variables. 60
Thank you