Let's not look at something called this reference earlier we did this in mission statement which basically 2
allowed an object to invoke one off its overloaded constructors that is from within one constructor 3
to access an overloaded constructor an object uses this in location statement. 4
This reference is also related to an object. 5
And that's what it is when to access variables are methods of an object we use it's object of friends 6
followed by dot operator which is in turn followed by the variable or method name. 7
But it's an object wants to access its own members. 8
That is not members of some other object but it's own members. 9
In that case it can use this reference. 10
And for that you would use the keyword this followed by dot operator which is then followed by the variable 11
Armatus name. 12
So this reference is basically an object reference but it is just that it's a reference to the current 13
object itself. 14
Not that we have this reference an object can also access its members directly. 15
So the next question would be why then use this reference when you can access the members directly. 16
The real use of this reference is when we want to access instance variables that are hidden by local 17
variables which are defined in a method or constructor. 18
Let's look at an example to see what it means for a variable to be hidden or shadowed. 19
Which is just another term for hidden. 20
In this example there are two instance variables ID and name. 21
There is also the method abrade profile which has two parameters. 22
New ID and name. 23
So one of the instance variables and one of the method parameters shared the same variable name. 24
CORNYN good to this. 25
We say that within the method update profile the method parameter name is hiding or shadowing the instance 26
variable called name on any reference to the variable name from within the abrade profile method is 27
actually a reference to the method parameter a Nonda instance variable. 28
So if you want to access the instance name from within the upgrade profile method you would have to 29
use this reference. 30
That is you would say this not name as shown here. 31
However if instance variable is not hidden by any local variable then the instance variable can be accessed 32
directly as we can see here in this particular statement where ID field is being assigned new ID just 33
know that this reference can also be used in constructors. 34
Also as I mentioned earlier this reference can be used to access methods in the current object. 35
However since there is no hiding of instance methods like in the case of instance variables you don't 36
have to do that. 37
You can access the methods directly. 38
So normally you would use this reference when you're accessing or need instance variables which are 39
hidden. 40
Finally note that this reference cannot be used in a static method. 41
If you recall our discussion on static methods we say that there are class methods and so they don't 42
deal with objects and that is they cannot access instance variables or instance methods defined in the 43
same class. 44
So this means that you cannot also use this reference within static methods as this reference is used 45
to access. 46
Instance variables are instance methods. 47
Let's actually take a look at this in the code. 48
Look here we are in the basics democracy. 49
Let's first define an instance with a bill here. 50
Let's just call it as ID and Ledgard have the default zero. 51
I just try to access it from the static method which we wrote earlier. 52
In fact all our methods here in this class are static. 53
So let's define a variable. 54
I. 55
And let's use this reference to access the instance variable. 56
So this should give us a compilation error. 57
So I'm in the basics directory let's just compile it. 58
So it's it's it gives a compilation error under-dressed non-static variable which means it's an instance 59
variable does. 60
Since this isn't a variable here it cannot be a reference from a static context where context implies 61
it's a static method static context implies static method. 62
So it's just it's not an instance variable. 63
This cannot be reference from in static context. 64
OK so we cannot access it. 65
And if you want we can also remove this iron. 66
We get a similar because. 67
Instance variables can be accessed directly but since it's a static method it still gives us a compilation 68
error on your chest non-static variable which means the instance variable ID. 69
So in this case it's an ID. 70
It cannot be referenced from as static context. 71
OK so we cannot access any state from static methods. 72
OK so that's about it. 73
And next we'll be doing a demo on and then we will use this reference. 74
So we will will be using this reference in the demo. 75
OK. 76
So that's about it. 77
And thank you