1

00:00:01,140  -->  00:00:07,740
Let's not look at something called this reference earlier we did this in mission statement which basically

2

00:00:07,740  -->  00:00:14,220
allowed an object to invoke one off its overloaded constructors that is from within one constructor

3

00:00:14,430  -->  00:00:19,450
to access an overloaded constructor an object uses this in location statement.

4

00:00:19,980  -->  00:00:21,960
This reference is also related to an object.

5

00:00:22,020  -->  00:00:30,300
And that's what it is when to access variables are methods of an object we use it's object of friends

6

00:00:30,450  -->  00:00:35,780
followed by dot operator which is in turn followed by the variable or method name.

7

00:00:36,240  -->  00:00:39,680
But it's an object wants to access its own members.

8

00:00:39,870  -->  00:00:43,930
That is not members of some other object but it's own members.

9

00:00:44,100  -->  00:00:47,160
In that case it can use this reference.

10

00:00:48,050  -->  00:00:53,930
And for that you would use the keyword this followed by dot operator which is then followed by the variable

11

00:00:53,930  -->  00:00:56,410
Armatus name.

12

00:00:56,610  -->  00:01:02,820
So this reference is basically an object reference but it is just that it's a reference to the current

13

00:01:02,880  -->  00:01:05,160
object itself.

14

00:01:05,910  -->  00:01:12,420
Not that we have this reference an object can also access its members directly.

15

00:01:12,720  -->  00:01:19,470
So the next question would be why then use this reference when you can access the members directly.

16

00:01:19,590  -->  00:01:25,380
The real use of this reference is when we want to access instance variables that are hidden by local

17

00:01:25,380  -->  00:01:29,640
variables which are defined in a method or constructor.

18

00:01:29,640  -->  00:01:35,230
Let's look at an example to see what it means for a variable to be hidden or shadowed.

19

00:01:35,310  -->  00:01:37,970
Which is just another term for hidden.

20

00:01:39,090  -->  00:01:43,430
In this example there are two instance variables ID and name.

21

00:01:43,530  -->  00:01:46,710
There is also the method abrade profile which has two parameters.

22

00:01:46,770  -->  00:01:48,710
New ID and name.

23

00:01:48,840  -->  00:01:53,960
So one of the instance variables and one of the method parameters shared the same variable name.

24

00:01:54,000  -->  00:01:56,620
CORNYN good to this.

25

00:01:56,660  -->  00:02:03,900
We say that within the method update profile the method parameter name is hiding or shadowing the instance

26

00:02:03,900  -->  00:02:10,620
variable called name on any reference to the variable name from within the abrade profile method is

27

00:02:10,620  -->  00:02:15,010
actually a reference to the method parameter a Nonda instance variable.

28

00:02:15,600  -->  00:02:21,360
So if you want to access the instance name from within the upgrade profile method you would have to

29

00:02:21,360  -->  00:02:22,900
use this reference.

30

00:02:23,100  -->  00:02:26,480
That is you would say this not name as shown here.

31

00:02:26,760  -->  00:02:32,370
However if instance variable is not hidden by any local variable then the instance variable can be accessed

32

00:02:32,370  -->  00:02:39,900
directly as we can see here in this particular statement where ID field is being assigned new ID just

33

00:02:39,900  -->  00:02:43,860
know that this reference can also be used in constructors.

34

00:02:43,920  -->  00:02:49,300
Also as I mentioned earlier this reference can be used to access methods in the current object.

35

00:02:49,830  -->  00:02:55,140
However since there is no hiding of instance methods like in the case of instance variables you don't

36

00:02:55,140  -->  00:02:56,160
have to do that.

37

00:02:56,460  -->  00:02:58,980
You can access the methods directly.

38

00:02:58,980  -->  00:03:03,330
So normally you would use this reference when you're accessing or need instance variables which are

39

00:03:03,330  -->  00:03:05,790
hidden.

40

00:03:05,790  -->  00:03:09,440
Finally note that this reference cannot be used in a static method.

41

00:03:09,810  -->  00:03:15,060
If you recall our discussion on static methods we say that there are class methods and so they don't

42

00:03:15,060  -->  00:03:20,790
deal with objects and that is they cannot access instance variables or instance methods defined in the

43

00:03:20,790  -->  00:03:22,510
same class.

44

00:03:22,530  -->  00:03:28,290
So this means that you cannot also use this reference within static methods as this reference is used

45

00:03:28,290  -->  00:03:29,060
to access.

46

00:03:29,070  -->  00:03:32,450
Instance variables are instance methods.

47

00:03:32,460  -->  00:03:35,930
Let's actually take a look at this in the code.

48

00:03:37,130  -->  00:03:39,380
Look here we are in the basics democracy.

49

00:03:39,870  -->  00:03:42,380
Let's first define an instance with a bill here.

50

00:03:42,840  -->  00:03:48,110
Let's just call it as ID and Ledgard have the default zero.

51

00:03:48,640  -->  00:03:53,720
I just try to access it from the static method which we wrote earlier.

52

00:03:53,760  -->  00:03:57,350
In fact all our methods here in this class are static.

53

00:03:57,360  -->  00:03:58,440
So let's define a variable.

54

00:03:58,440  -->  00:03:59,330
I.

55

00:04:00,120  -->  00:04:05,350
And let's use this reference to access the instance variable.

56

00:04:05,520  -->  00:04:08,830
So this should give us a compilation error.

57

00:04:09,840  -->  00:04:13,050
So I'm in the basics directory let's just compile it.

58

00:04:13,770  -->  00:04:19,560
So it's it's it gives a compilation error under-dressed non-static variable which means it's an instance

59

00:04:19,560  -->  00:04:21,050
variable does.

60

00:04:21,270  -->  00:04:28,140
Since this isn't a variable here it cannot be a reference from a static context where context implies

61

00:04:28,190  -->  00:04:31,720
it's a static method static context implies static method.

62

00:04:31,830  -->  00:04:34,020
So it's just it's not an instance variable.

63

00:04:34,020  -->  00:04:36,900
This cannot be reference from in static context.

64

00:04:36,900  -->  00:04:39,210
OK so we cannot access it.

65

00:04:39,360  -->  00:04:41,740
And if you want we can also remove this iron.

66

00:04:41,790  -->  00:04:45,360
We get a similar because.

67

00:04:45,390  -->  00:04:50,730
Instance variables can be accessed directly but since it's a static method it still gives us a compilation

68

00:04:50,730  -->  00:04:55,240
error on your chest non-static variable which means the instance variable ID.

69

00:04:55,350  -->  00:04:56,940
So in this case it's an ID.

70

00:04:57,000  -->  00:04:59,550
It cannot be referenced from as static context.

71

00:04:59,580  -->  00:05:04,420
OK so we cannot access any state from static methods.

72

00:05:04,490  -->  00:05:05,670
OK so that's about it.

73

00:05:05,720  -->  00:05:10,370
And next we'll be doing a demo on and then we will use this reference.

74

00:05:10,530  -->  00:05:14,210
So we will will be using this reference in the demo.

75

00:05:14,310  -->  00:05:15,000
OK.

76

00:05:15,000  -->  00:05:16,180
So that's about it.

77

00:05:16,190  -->  00:05:16,840
And thank you
