Difference between revisions of "Java/C2/Using-this-keyword/English-timed"

From Script | Spoken-Tutorial
Jump to: navigation, search
 
(14 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
{| border=1
 
{| border=1
|| ''Time'''
+
|| '''Time'''
 
|| '''Narration'''
 
|| '''Narration'''
  
 
|-
 
|-
 
| 00:02
 
| 00:02
|  Welcome to the Spoken Tutorial on '''using this keyword''' in '''java'''.  
+
|  Welcome to the Spoken Tutorial on '''Using "this" keyword''' in '''java'''.  
  
 
|-
 
|-
 
| 00:07  
 
| 00:07  
|  In this tutorial we will learn  
+
|  In this tutorial, we will learn:
  
 
|-
 
|-
 
| 00:09
 
| 00:09
|About use of '''this''' keyword  
+
| About use of "this" keyword  
  
 
|-
 
|-
 
| 00:11
 
| 00:11
| To use '''this''' keyword with '''fields'''
+
|To use "this" keyword with '''fields'''
  
 
|-
 
|-
 
| 00:14
 
| 00:14
| To use '''this '''keyword for chaining of '''constructors'''.
+
|To use '''this''' keyword for chaining of '''constructors'''.  
 
+
 
+
 
+
  
 
|-
 
|-
 
| 00:17
 
| 00:17
|   Here we are using  
+
| Here we are using:
 
+
'''Ubuntu version 11.10'''
* Ubuntu version 11.10  
+
'''jdk 1.6'''
jdk 1.6
+
'''Eclipse 3.7.0'''
* Eclipse 3.7.0
+
 
+
 
+
  
 
|-
 
|-
 
|  00:28
 
|  00:28
| To follow this tutorial you must know  
+
| To follow this tutorial, you must know  
  
 
|-
 
|-
 
| 00:30
 
| 00:30
|  how to create a ''' constructor''' in '''java''' using '''eclipse'''.
+
|  how to create a '''constructor''' in java, using '''eclipse'''.  
 
+
  
 
|-
 
|-
 
| 00:34
 
| 00:34
|  If not, for relevant tutorials please visit our website which is as shown,
+
|  If not, for relevant tutorials, please visit our website which is as shown. http://www.spoken-tutorial.org  
 
+
('''http'''://'''www.spoken'''-'''tutorial.org''')
+
  
 
|-
 
|-
 
| 00:40
 
| 00:40
|Now we will see the use of this '''key word'''
+
|Now we will see the use of '''this''' keyword.
  
 
|-
 
|-
 
| 00:44
 
| 00:44
| Within a '''constructor''', '''this''' is a reference to the current '''object'''.  
+
| Within a constructor, "this" is a reference to the current '''object'''.  
 
+
  
 
|-
 
|-
 
| 00:48
 
| 00:48
|  We can refer any member of the current '''object''' within a''' constructor''' using '''this'''.
+
|  We can refer any member of the current '''object''', within a '''constructor''', using "this".  
  
 
|-
 
|-
 
| 00:55
 
| 00:55
|Now we will see the use of '''this''' keyword with '''fields'''.  
+
|Now we will see the use of '''this''' keyword with fields.   
  
 
|-
 
|-
 
| 01:00
 
| 01:00
| '''this '''keyword helps us to avoid name conflicts.
+
| '''this''' keyword helps us to avoid name conflicts.  
 
+
  
 
|-
 
|-
 
| 01:07
 
| 01:07
|  We can see such an example here.
+
|  We can see such an example here.  
 
+
 
|-
 
|-
 
|  01:10
 
|  01:10
|  For that let us open '''Eclipse'''.
+
|  For that, let us open '''Eclipse'''.  
 
+
  
 
|-
 
|-
 
| 01:17
 
| 01:17
|  Open the '''Student class''' we had created in the earlier tutorial.
+
|  Open the '''Student class''' we had created in the earlier tutorial.  
 
+
  
 
|-
 
|-
 
| 01:23
 
| 01:23
|  Comment the '''default constructor ''' comment the '''constructor''' with 1 parameter.
+
|  Comment the '''default constructor''', comment the '''constructor''' with 1 parameter.  
 
+
  
 
|-
 
|-
 
| 01:40
 
| 01:40
|  Also comment the code for creating the first two '''objects'''.
+
|  Also comment the code for creating the first two '''objects'''.  
 
+
  
 
|-
 
|-
 
| 02:03
 
| 02:03
|  Now, notice the '''parameterized constructor.'''
+
|  Now, notice the '''parameterized constructor'''.
 
+
  
 
|-
 
|-
 
| 02:11
 
| 02:11
| '''the_roll_number''' and '''the_name '''are the arguments passed to the constructor.
+
| '''the_roll_number''' and '''the_name''' are the arguments passed to the constructor.  
 
+
  
 
|-
 
|-
 
| 02:20
 
| 02:20
|  '''roll_number''' and '''name '''are the instance variables.
+
|  '''roll_number''' and '''name''' are the '''instance''' variables.  
 
+
  
 
|-
 
|-
 
| 02:26
 
| 02:26
|  Now, let me change the arguments to '''roll_number''' and '''name''' itself.
+
|  Now, let me change the arguments to '''roll_number''' and '''name''' itself.  
 
+
  
 
|-
 
|-
 
| 02:39
 
| 02:39
|  So inside the '''constructor''' we have:
+
|  So inside the constructor we have:  
  
 
|-
 
|-
 
| 02:42
 
| 02:42
|  '''roll_number '''equal to''' roll_number''' and '''name '''equal to''' name.'''
+
|  '''roll_number''' equal to '''roll_number''' and '''name''' equal to '''name'''.
 
+
  
 
|-
 
|-
 
| 02:55
 
| 02:55
|Now '''Save '''and '''run '''the file.So press'''Ctrl ,S'''and '''Ctrl, F11'''
+
|Now '''Save''' and '''run''' the file. So press '''Ctrl, S''' and '''Ctrl, F11'''.
  
 
|-
 
|-
 
|  03:04
 
|  03:04
|  We get the output as follows:
+
|  We get the output as follows:  
 
+
+
  
 
|-
 
|-
 
| 03:07
 
| 03:07
|   I am a Parameterized Constructor
+
| ''' I am a parameterized constructor'''
''' 0
+
'''0'''
null'''
+
''' null'''
  
 
|-
 
|-
 
| 03:12
 
| 03:12
| Now come back to the code.
+
| Now come back to the code.  
  
 
|-
 
|-
 
| 03:17  
 
| 03:17  
| We see 2 warnings in the code.
+
| We see 2 warnings in the code.  
 
|-
 
|-
 
| 03:20
 
| 03:20
|  Hover your mouse over the warning symbol.
+
|  Hover your mouse over the warning symbol.  
  
 
|-
 
|-
 
| 03:23
 
| 03:23
|We can see '''The assignment to the variable roll_number has no effect.'''
+
|We can see '''The assignment to the variable roll_number has no effect.'''
 
+
'''
 
|-
 
|-
 
| 03:29
 
| 03:29
| And '''The assignment to the variable name has no effect.'''
+
| And '''The assignment to the variable name has no effect'''.
 
+
  
 
|-
 
|-
 
| 03:33
 
| 03:33
|  This is because in the '''constructor roll_number and name''' are '''local variables'''.
+
|  This is because, in the '''constructor''', 'roll_number' and 'name' are ''local variables''.  
 
+
  
 
|-
 
|-
 
| 03:40
 
| 03:40
'''Local variables''' are '''variables''' that are accessible within the '''method''' or '''block'''.  
+
|  ''Local variables'' are variables that are accessible within the method or block.  
 
+
 
+
  
 
|-
 
|-
 
| 03:47
 
| 03:47
|  Here, '''roll_number''' and '''name''' will be initialized to '''11''' and '''Raju'''.
+
|  Here, 'roll_number' and 'name' will be initialized to 11 and "Raju"
 
+
 
|-
 
|-
 
| 03:54
 
| 03:54
| Because we have passed the values '''11''' and '''Raju'''the constructor.
+
| because we have passed the values 11 and "Raju" into the constructor.  
  
 
|-
 
|-
 
| 04:01
 
| 04:01
| But once they come out of the '''constructor''', it is not accessible.
+
| But once they come out of the constructor, it is not accessible.  
 
+
 
+
  
 
|-
 
|-
 
| 04:06
 
| 04:06
| Then the only '''roll_number''' and '''name''' we know are the '''instance variables'''.
+
| Then the only '''roll_number''' and '''name''' we know, are the instance variables.  
 
+
 
+
  
 
|-
 
|-
 
| 04:13
 
| 04:13
|  They have been initialized to '''0 '''and '''null '''already once the '''object''' is created.
+
|  They have been initialized to '0' and ''null' already once the object is created.  
 
+
 
+
  
 
|-
 
|-
 
| 04:18
 
| 04:18
|  So we got the output as '''0 '''and '''null'''.
+
|  So we got the output as '''0''' and '''null'''.  
 
+
 
|-
 
|-
 
| 04:21
 
| 04:21
|  Now, let us make a small change inside the constructor.
+
|  Now, let us make a small change inside the '''constructor'''.  
 
+
 
+
  
 
|-
 
|-
 
| 04:29
 
| 04:29
| So type '''this '''''dot '''''roll_number equal to roll_number'''.
+
| So, type: '''this''' dot '''roll_number''' equal to '''roll_number'''.  
 
+
 
+
  
 
|-
 
|-
 
| 04:37
 
| 04:37
|  And''' this '''''dot '''''name equal to name'''.
+
|  And, '''this''' dot '''name''' equal to '''name'''.  
 
+
 
+
 
+
 
|-
 
|-
 
| 04:44
 
| 04:44
|  Now '''save '''and '''run''' the file. So press '''ctrl, S''' And '''Ctrl, F11''' keys
+
|  Now '''save''' and '''run''' the file. So press '''ctrl, S''' And '''Ctrl, F11''' keys.
  
 
|-
 
|-
 
| 04:51
 
| 04:51
| We get the output as
+
| We get the output as:
 
+
 
+
  
 
|-
 
|-
 
| 04:53
 
| 04:53
I am Parameterized Constructor
+
|   
'''11 and
+
'''I am parameterized constructor'''
Raju'''
+
  
 +
'''11''' and
 +
 +
'''Raju'''
  
 
|-  
 
|-  
 
| 04:58
 
| 04:58
|  This is because '''this '''''dot '''''roll_number '''and '''this '''''dot '''''name''' refers to the '''instance variables''' '''roll_number''' and '''name.'''
+
|  This is because '''this''' dot '''roll_number''' and '''this''' dot '''name''' refer to the instance variables 'roll_number' and 'name'.
 
+
 
+
  
 
|-
 
|-
 
| 05:12
 
| 05:12
|And here '''roll_number '''and '''name '''are the arguments passed in the method.
+
|And here '''roll_number''' and '''name''' are the arguments, passed in the '''method'''.  
 
+
 
+
  
 
|-
 
|-
 
| 05:19
 
| 05:19
|  To avoid confliction between '''local''' and '''instance''' variables we use '''this '''keyword.
+
|  To avoid conflict between '''local''' and '''instance''' variables we use "this" keyword.  
  
 
|-
 
|-
 
| 05:29
 
| 05:29
|  Now we will see the use of '''this keywords''' for chaining of constructor.
+
|  Now, we will see the use of "this" keyword for chaining of '''constructor'''.  
  
 
|-
 
|-
 
| 05:34
 
| 05:34
|  We can use '''this '''keyword inside a '''constructor''' to call another one.  
+
|  We can use this keyword inside a '''constructor''' to call another one.  
 
+
 
+
  
 
|-
 
|-
 
| 05:39
 
| 05:39
|  The '''constructors''' must be in the same '''class.'''
+
|  The '''constructors''' must be in the same class.
 
+
 
+
  
 
|-
 
|-
 
| 05:43
 
| 05:43
| This is called '''explicit constructor invocation'''.
+
| This is called '''explicit constructor invocation'''.
 
|-
 
|-
 
| 05:46
 
| 05:46
  
| So let us come back to the''' Student '''class which we created.
+
| So, let us come back to the '''Student''' class which we created.  
  
 
|-
 
|-
 
|  05:53
 
|  05:53
|Now Remove the comments.
+
|Now remove the comments.  
  
 
|-
 
|-
 
| 06:28
 
| 06:28
| Now comment the part to assign the '''instance variables to their values in the first two constructors.'''
+
| Now, comment the part to assign the instance variables to their values in the first two '''constructors'''.
 
+
 
+
  
 
|-
 
|-
 
| 06:52
 
| 06:52
|Then Comment the part which creates the''' '''second and third '''objects.'''
+
|Then '''comment''' the part which creates the second and third objects.
  
 
|-
 
|-
 
|  07:08
 
|  07:08
| Now let us first come to the   constructor with no parameters.
+
| Now, let us first come to the '''constructor''' with no parameters.  
 
+
 
+
  
 
|-
 
|-
 
| 07:16
 
| 07:16
|  After   curly brackets type '''this''' within brackets '''11''' and ''semicolon''.
+
|  After curly brackets type: '''this''' within brackets '''11''' and semicolon.  
 
+
 
+
+
 
|-
 
|-
 
|  07:28
 
|  07:28
| Inside the second '''constructor''' type '''this''' within brackets '''11''' ''comma'' within double quotes '''Raju''' ''semicolon''.
+
| Inside the second constructor, type: '''this''' within brackets '''11''' comma within double quotes "Raju" semicolon.  
 
   
 
   
 
 
|-
 
|-
 
| 07:42
 
| 07:42
| Now '''Save''' and '''Run''' the file. So press '''Ctrl,S''' and '''Ctrl , F11'''.
+
| Now, '''Save''' and '''Run''' the file. So, press '''Ctrl, S''' and '''Ctrl , F11'''.  
  
 
|-
 
|-
 
| 07:49
 
| 07:49
|  We get the output as  
+
|  We get the output as
 
+
 
+
  
 
|-
 
|-
 
| 07:51
 
| 07:51
|   '''I am a Parameterized Constructor'''
+
| '''I am a parameterized constructor'''
  
 
|-
 
|-
 
| 07:54
 
| 07:54
|   '''I am a constructor with a single parameter'''
+
| '''I am a constructor with a single parameter'''
  
 
|-
 
|-
 
| 07:57
 
| 07:57
|   '''I am Default Constructor'''
+
| '''I am Default Constructor'''
  
'''11''' and
+
'''11''' and  
  
 
'''Raju'''
 
'''Raju'''
Line 337: Line 277:
 
|-
 
|-
 
|  08:02
 
|  08:02
| Now, I will explain the output.
+
| Now, I will explain the output.  
 
+
 
+
  
 
|-
 
|-
 
| 08:08
 
| 08:08
|  When the object is created, the respective constructor gets called.
+
|  When the object is created, the respective '''constructor''' gets called.  
 
+
 
+
  
 
|-
 
|-
 
| 08:13
 
| 08:13
|  The '''constructor''' here is the '''no argument constructor'''.
+
|  The constructor here is the ''no argument'' '''constructor'''.
 
+
 
+
  
 
|-
 
|-
 
| 08:20
 
| 08:20
 
|  The control comes to the first line in the '''constructor'''.
 
|  The control comes to the first line in the '''constructor'''.
 
 
  
 
|-
 
|-
 
| 08:24
 
| 08:24
| It encounters the '''this '''''within brackets '''''11''' statement.
+
| It encounters the '''this''' within brackets '''11''' statement.  
 
+
 
+
  
 
|-
 
|-
 
| 08:26
 
| 08:26
|  Hence it calls the constructor that accepts single integer argument.
+
|  Hence it calls the '''constructor''' that accepts single integer argument.  
 
+
 
+
  
 
|-
 
|-
 
| 08:36
 
| 08:36
|  Then the control comes to '''this '''''within brackets''''' 11 '''''comma '''''Raju.'''
+
|  Then the control comes to '''this''' within brackets '''11''' comma '''Raju'''.
 
+
 
+
  
 
|-
 
|-
 
| 08:44
 
| 08:44
|  Hence it calls the constructor that accepts 1 '''integer''' and 1 '''String''' argument.
+
|  Hence it calls the '''constructor''' that accepts '''1 integer''' and '''1 String''' argument.  
 
+
 
+
  
 
|-
 
|-
 
| 08:53
 
| 08:53
|  So this '''constructor''' is executed and we get the output as '''I am Parameterized Constructor'''.
+
|  So, this '''constructor''' is executed and we get the output as '''I am a parameterized constructor'''.  
 
+
 
+
  
 
|-
 
|-
 
| 09:02
 
| 09:02
|Now the instance variables will be initialized to '''11''' and '''Raju.'''As we have passed.
+
|Now the instance variables will be initialized to '''11''' and '''Raju''' as we have passed.  
 
+
 
+
  
 
|-
 
|-
 
| 09:11
 
| 09:11
|  Now, the control goes back to the calling '''constructor'''.
+
|  Now, the control goes back to the calling '''constructor'''.  
 
+
 
+
  
 
|-
 
|-
 
| 09:16
 
| 09:16
|  So the second''' constructor ''' gets executed.
+
|  So, the second constructor gets executed.  
 
+
 
+
  
 
|-
 
|-
 
| 09:19
 
| 09:19
|  We get the output as''' I am constructor with a single parameter.'''
+
|  We get the output as '''I am constructor with a single parameter'''.
 
+
 
+
  
 
|-
 
|-
 
| 09:25  
 
| 09:25  
|  Then, the control goes to the first constructor and executes it.
+
|  Then, the control goes to the first '''constructor''' and executes it.  
 
+
 
+
  
 
|-
 
|-
 
| 09:30
 
| 09:30
|  So we get output as''' I am a default constructor.'''
+
|  So, we get output as '''I am a default constructor'''.
 
+
 
+
  
 
|-
 
|-
 
| 09:36
 
| 09:36
|  Then '''studentDetail method''' is executed.
+
|  Then '''studentDetail''' method is executed.  
 
+
 
+
  
 
|-
 
|-
 
| 09:42
 
| 09:42
|  So, we get '''11 '''and '''Raju'''.
+
|  So, we get '''11''' and '''Raju'''.  
  
 
|-
 
|-
 
|  09:45
 
|  09:45
|  Now, let us make a small change.
+
|  Now, let us make a small change.  
 
+
 
+
  
 
|-
 
|-
 
| 09:47
 
| 09:47
| Make the '''this''' statement the last one in the constructor.
+
| Make the '''this''' statement the last one in the '''constructor'''.  
 
+
 
+
  
 
|-
 
|-
 
| 10:00
 
| 10:00
|  We get a compiler error.
+
|  We get a compiler error.  
 
+
 
+
  
 
|-
 
|-
 
| 10:03
 
| 10:03
|  Hover the mouse over the error symbol.
+
|  Hover the mouse over the error symbol.  
 
+
 
+
  
 
|-
 
|-
 
| 10:06
 
| 10:06
|  We get the error as:
+
|  We get the error as:  
 
+
 
+
  
 
|-
 
|-
 
| 10:07
 
| 10:07
|  '''Constructor''' call must be the first statement in the '''constructor'''.
+
|  '''Constructor call must be the first statement in a constructor'''.
  
 
|-
 
|-
 
|  10:12
 
|  10:12
|   So we must make it the first line of the '''constructor.'''
+
| So, we must make it the first line of the '''constructor'''.
  
 
|-
 
|-
 
|  10:16
 
|  10:16
| So make it the first line of the '''constructor.'''
+
| So, make it the first line of the '''constructor'''.
 
+
 
+
  
 
|-
 
|-
 
| 10:27
 
| 10:27
|  Now we can see that the error has gone.
+
|  Now we can see that the error has gone.  
  
 
|-
 
|-
 
|  10:31
 
|  10:31
|  So in this tutorial, we   learnt  
+
|  So, in this tutorial, we learnt:
 
+
  
 
|-
 
|-
 
| 10:35
 
| 10:35
| To use''' this''' keyword with''' fields.'''
+
|The use of '''this''' keyword with '''fields'''.
 
+
  
 
|-
 
|-
 
| 10:38
 
| 10:38
| To use''' this''' keyword for chaining '''constructors'''
+
|To use '''this''' keyword for chaining '''constructors'''
 
+
  
 
|-
 
|-
 
| 10:41
 
| 10:41
|  How '''this '''keyword should be used within a''' constructor.'''
+
|  How '''this''' keyword should be used within a '''constructor'''.
 
+
 
+
 
+
  
 
|-
 
|-
 
|  10:45
 
|  10:45
|  For self assessment, in the '''Employee class''' created earlier:
+
|  For self assessment, in the '''Employee''' '''class''' created earlier:  
 
+
  
 
|-
 
|-
 
| 10:49
 
| 10:49
|  Create a '''constructor''' with two parameters
+
|  Create a '''constructor''' with two parameters.
  
 
|-
 
|-
 
| 10:52
 
| 10:52
|  Use''' this '''keyword to initialize the''' instance variables .'''
+
|  Use '''this''' keyword to initialize the '''instance variables''' .
  
 
|-
 
|-
| 10:57
+
| 10:57  
| Also create a '''constructor''' with 1 and no parameters.
+
| Also, create a '''constructor''' with 1 and no parameters.  
  
 
|-
 
|-
 
| 11:01
 
| 11:01
|  Try chaining the '''constructors '''using '''this''' as explained in the tutorial.
+
|  Try chaining the '''constructors''' using '''this''' as explained in the tutorial.  
 
+
 
+
  
 
|-
 
|-
 
|  11:07
 
|  11:07
|  To know more about the Spoken Tutorial Project
+
|  To know more about the Spoken Tutorial project,
  
 
|-
 
|-
 
| 11:09
 
| 11:09
|   Watch the video available at the following link:[http://spoken-tutorial.org/What_is_a_Spoken_Tutorial http://spoken-][http://spoken-tutorial.org/What_is_a_Spoken_Tutorial tutorial.org/What_is_a_Spoken_Tutorial]
+
| watch the video available at the following link [http://spoken-tutorial.org/What_is_a_Spoken_Tutorial].
  
 
|-
 
|-
 
| 11:12
 
| 11:12
|   It summarizes the Spoken Tutorial project  
+
| It summarizes the Spoken Tutorial project.
 
+
 
+
 
|-
 
|-
 
| 11:16
 
| 11:16
|  If you do not have good bandwidth, you can download and watch it  
+
|  If you do not have good bandwidth, you can download and watch it.
 
+
 
+
  
 
|-
 
|-
 
|  11:19  
 
|  11:19  
|  The Spoken Tutorial Project Team
+
|  The Spoken Tutorial project team:
 
+
 
+
 
|-
 
|-
 
| 11:23
 
| 11:23
|  Conducts workshops using spoken tutorials  
+
|  Conducts workshops using spoken tutorials.
 
+
  
 
|-
 
|-
 
| 11:26
 
| 11:26
| Gives certificates for those who pass an online test  
+
| Gives certificates to those who pass an online test.
 
+
  
 
|-
 
|-
 
| 11:30
 
| 11:30
|  For more details, please write to contact@spoken-tutorial.org  
+
|  For more details, please write to contact@spoken-tutorial.org.
 
+
 
+
 
+
 
|-
 
|-
 
|  11:36
 
|  11:36
|  Spoken Tutorial Project is a part of the Talk to a Teacher project  
+
|  Spoken Tutorial Project is a part of the Talk to a Teacher project.
 
+
  
 
|-
 
|-
 
| 11:40
 
| 11:40
|  It is supported by the National Mission on Education through ICT, MHRD, Government of India  
+
|  It is supported by the National Mission on Education through ICT, MHRD, Government of India.
 
+
 
+
 
|-
 
|-
 
| 11:46
 
| 11:46
|  More information on this Mission is available at  
+
|  More information on this mission is available at http://spoken-tutorial.org/NMEICT-Intro.
 
+
* [http://spoken-tutorial.org/NMEICT-Intro http://spoken-tutorial.org/NMEICT-Intro]
+
 
+
 
+
  
 
|-
 
|-
 
| 11:55
 
| 11:55
 
| Thus We have come to the end of this tutorial.
 
| Thus We have come to the end of this tutorial.
 
 
 
 
 
  
 
|-
 
|-
 
| 11:58
 
| 11:58
|  This is Arya Ratish signing off. Thanks for joining.
+
|  This is Arya Ratish, signing off. Thanks for joining.

Latest revision as of 19:35, 20 February 2017

Time Narration
00:02 Welcome to the Spoken Tutorial on Using "this" keyword in java.
00:07 In this tutorial, we will learn:
00:09 About use of "this" keyword
00:11 To use "this" keyword with fields
00:14 To use this keyword for chaining of constructors.
00:17 Here we are using:

Ubuntu version 11.10 jdk 1.6 Eclipse 3.7.0

00:28 To follow this tutorial, you must know
00:30 how to create a constructor in java, using eclipse.
00:34 If not, for relevant tutorials, please visit our website which is as shown. http://www.spoken-tutorial.org
00:40 Now we will see the use of this keyword.
00:44 Within a constructor, "this" is a reference to the current object.
00:48 We can refer any member of the current object, within a constructor, using "this".
00:55 Now we will see the use of this keyword with fields.
01:00 this keyword helps us to avoid name conflicts.
01:07 We can see such an example here.
01:10 For that, let us open Eclipse.
01:17 Open the Student class we had created in the earlier tutorial.
01:23 Comment the default constructor, comment the constructor with 1 parameter.
01:40 Also comment the code for creating the first two objects.
02:03 Now, notice the parameterized constructor.
02:11 the_roll_number and the_name are the arguments passed to the constructor.
02:20 roll_number and name are the instance variables.
02:26 Now, let me change the arguments to roll_number and name itself.
02:39 So inside the constructor we have:
02:42 roll_number equal to roll_number and name equal to name.
02:55 Now Save and run the file. So press Ctrl, S and Ctrl, F11.
03:04 We get the output as follows:
03:07 I am a parameterized constructor

0 null

03:12 Now come back to the code.
03:17 We see 2 warnings in the code.
03:20 Hover your mouse over the warning symbol.
03:23 We can see The assignment to the variable roll_number has no effect.

03:29 And The assignment to the variable name has no effect.
03:33 This is because, in the constructor, 'roll_number' and 'name' are local variables.
03:40 Local variables are variables that are accessible within the method or block.
03:47 Here, 'roll_number' and 'name' will be initialized to 11 and "Raju"
03:54 because we have passed the values 11 and "Raju" into the constructor.
04:01 But once they come out of the constructor, it is not accessible.
04:06 Then the only roll_number and name we know, are the instance variables.
04:13 They have been initialized to '0' and null' already once the object is created.
04:18 So we got the output as 0 and null.
04:21 Now, let us make a small change inside the constructor.
04:29 So, type: this dot roll_number equal to roll_number.
04:37 And, this dot name equal to name.
04:44 Now save and run the file. So press ctrl, S And Ctrl, F11 keys.
04:51 We get the output as:
04:53

I am parameterized constructor

11 and

Raju

04:58 This is because this dot roll_number and this dot name refer to the instance variables 'roll_number' and 'name'.
05:12 And here roll_number and name are the arguments, passed in the method.
05:19 To avoid conflict between local and instance variables we use "this" keyword.
05:29 Now, we will see the use of "this" keyword for chaining of constructor.
05:34 We can use this keyword inside a constructor to call another one.
05:39 The constructors must be in the same class.
05:43 This is called explicit constructor invocation.
05:46 So, let us come back to the Student class which we created.
05:53 Now remove the comments.
06:28 Now, comment the part to assign the instance variables to their values in the first two constructors.
06:52 Then comment the part which creates the second and third objects.
07:08 Now, let us first come to the constructor with no parameters.
07:16 After curly brackets type: this within brackets 11 and semicolon.
07:28 Inside the second constructor, type: this within brackets 11 comma within double quotes "Raju" semicolon.
07:42 Now, Save and Run the file. So, press Ctrl, S and Ctrl , F11.
07:49 We get the output as:
07:51 I am a parameterized constructor
07:54 I am a constructor with a single parameter
07:57 I am Default Constructor

11 and

Raju

08:02 Now, I will explain the output.
08:08 When the object is created, the respective constructor gets called.
08:13 The constructor here is the no argument constructor.
08:20 The control comes to the first line in the constructor.
08:24 It encounters the this within brackets 11 statement.
08:26 Hence it calls the constructor that accepts single integer argument.
08:36 Then the control comes to this within brackets 11 comma Raju.
08:44 Hence it calls the constructor that accepts 1 integer and 1 String argument.
08:53 So, this constructor is executed and we get the output as I am a parameterized constructor.
09:02 Now the instance variables will be initialized to 11 and Raju as we have passed.
09:11 Now, the control goes back to the calling constructor.
09:16 So, the second constructor gets executed.
09:19 We get the output as I am constructor with a single parameter.
09:25 Then, the control goes to the first constructor and executes it.
09:30 So, we get output as I am a default constructor.
09:36 Then studentDetail method is executed.
09:42 So, we get 11 and Raju.
09:45 Now, let us make a small change.
09:47 Make the this statement the last one in the constructor.
10:00 We get a compiler error.
10:03 Hover the mouse over the error symbol.
10:06 We get the error as:
10:07 Constructor call must be the first statement in a constructor.
10:12 So, we must make it the first line of the constructor.
10:16 So, make it the first line of the constructor.
10:27 Now we can see that the error has gone.
10:31 So, in this tutorial, we learnt:
10:35 The use of this keyword with fields.
10:38 To use this keyword for chaining constructors
10:41 How this keyword should be used within a constructor.
10:45 For self assessment, in the Employee class created earlier:
10:49 Create a constructor with two parameters.
10:52 Use this keyword to initialize the instance variables .
10:57 Also, create a constructor with 1 and no parameters.
11:01 Try chaining the constructors using this as explained in the tutorial.
11:07 To know more about the Spoken Tutorial project,
11:09 watch the video available at the following link [1].
11:12 It summarizes the Spoken Tutorial project.
11:16 If you do not have good bandwidth, you can download and watch it.
11:19 The Spoken Tutorial project team:
11:23 Conducts workshops using spoken tutorials.
11:26 Gives certificates to those who pass an online test.
11:30 For more details, please write to contact@spoken-tutorial.org.
11:36 Spoken Tutorial Project is a part of the Talk to a Teacher project.
11:40 It is supported by the National Mission on Education through ICT, MHRD, Government of India.
11:46 More information on this mission is available at http://spoken-tutorial.org/NMEICT-Intro.
11:55 Thus We have come to the end of this tutorial.
11:58 This is Arya Ratish, signing off. Thanks for joining.

Contributors and Content Editors

Arya Ratish, Gaurav, Kaushik Datta, PoojaMoolya, Sandhya.np14, Sneha