Java Essentials - This keyword in java
4K views
Oct 24, 2024
Java Essentials - This keyword in java
View Video Transcript
0:00
This keyword in Java
0:02
This keyword will actually refer the current object defined under the class
0:08
And that's why this keyword will be used in our Java code in a very frequent basis
0:13
What are the purposes of this keyword? This is a reference variable which refers to the current object defined under the class
0:23
This keyword can refer to the members also. The members of the class can also be accessed using this
0:29
using this this keyword so these within bracket parameter is referred to the
0:34
constructor of the current class so this is very interesting that we'll be
0:39
using our this keyword in our Java code so this is a reference variable
0:44
which is actually referring which is have actually holding the reference of the
0:48
current object and object is having a certain state and the behavior so here it is
0:54
containing member variables containing some values and the respective of permitted operations mentioned in the member methods
1:01
So let us go for one practical demonstration to understand what is the purpose of this this keyword in our Java code in this program we shall discuss about the keyword this so here we have
1:14
defined one class the name of the class is my class having private I NTA
1:18
comma B two instance variable so public my class so there is a constructor which
1:24
is parameter as constructed having I NTA and B as input parameters now the thing
1:29
is that these input parameters A and B and also the the instance variables are a and b so if we want to initialize this a with this input
1:38
parameter a then that may produce some ambiguity if you write a is equal to a so that is
1:44
ambiguous so that's why it is better to write this dot a is equal to a here this is
1:49
actually indicating the object which will be defined under the class my class so that
1:55
object named dot a in this way i'm accessing the instance variable a is equal to a and
2:00
this is a which is on the right side of this assignment statement is nothing but the input parameter a similarly this dot B is
2:09
equal to B here this is my non parameter as constructor the constructor name will be same as that of the class name that is my class Here also it is my class and it is non And here we are calling the these uh
2:22
0.0, that means it will call the parameter as constructor with the value
2:26
zero and zero as two parameters. So in this way, this here, this is actually referring the constructor
2:34
That is the parameter as constructor because we are passing this 0.0 as the parameters
2:39
We're having the set data. and it is having this A and B as input parameters and this dot A is equal to A and this dot B is equal to B
2:48
That means the object defined under the class My class that object name
2:53
A is equal to input parameter A and object name dot B will be equal to input parameter B here
3:01
Display is actually displaying the value of A and B respectively. Now here we are having this keyword this the class we are defining containing the main function the my
3:11
class is having the object that is the my object is equal to new my class so the new is
3:15
the operator and this my class is actually the constructor with the help of which this my object has got defined and now we are going for my object display So display will print this A and B We know that whenever you are calling this constructor this constructed is non parameterized that means this particular constructor has been called
3:35
So it will pass this, it will call this parameters constructed with the value 0 and 0
3:41
So what will happen this A and B will be initialized with the value 0 and 0
3:46
So this my object. Dot display will print the value of A and B as 0 and 0
3:52
Next time we are calling this, that is a my object. That set data, set data will initialize this A and B with the respective values
4:02
And then my object. Dot display will print this 23 and 46 as the result as outcome
4:10
So in this way, we have discussed that how this constructor can be defined and what are the different types of constructors
4:16
are there and also we have we have shown you that how to how to use the keyword
4:21
this and this is actually referring the object defined under the class so it
4:27
can also call it as a dummy object name thanks for watching this video
#Computer Education
#Education
#Java (Programming Language)