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