Difference between revisions of "Java/C2/Using-this-keyword/English-timed"
From Script | Spoken-Tutorial
PoojaMoolya (Talk | contribs) |
|||
(7 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 ''' | + | | 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 | + | | About use of "this" keyword |
|- | |- | ||
| 00:11 | | 00:11 | ||
− | | | + | |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: |
− | + | '''Ubuntu version 11.10''' | |
− | + | '''jdk 1.6''' | |
− | + | '''Eclipse 3.7.0''' | |
− | + | ||
|- | |- | ||
| 00:28 | | 00:28 | ||
− | | | + | | To follow this tutorial, you must know |
|- | |- | ||
| 00:30 | | 00:30 | ||
− | | how to create a '''constructor''' in | + | | 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 |
− | |||
|- | |- | ||
| 00:40 | | 00:40 | ||
− | |Now we will see the use of this | + | |Now we will see the use of '''this''' keyword. |
|- | |- | ||
| 00:44 | | 00:44 | ||
− | | Within a constructor, | + | | 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 | + | | We can refer any member of the current '''object''', within a '''constructor''', using "this". |
|- | |- | ||
Line 69: | Line 67: | ||
|- | |- | ||
| 01:10 | | 01:10 | ||
− | | For that let us open '''Eclipse'''. | + | | For that, let us open '''Eclipse'''. |
|- | |- | ||
Line 77: | Line 75: | ||
|- | |- | ||
| 01:23 | | 01:23 | ||
− | | Comment the '''default constructor''' comment the '''constructor''' with 1 parameter. | + | | Comment the '''default constructor''', comment the '''constructor''' with 1 parameter. |
|- | |- | ||
Line 93: | Line 91: | ||
|- | |- | ||
| 02:20 | | 02:20 | ||
− | | '''roll_number''' and '''name''' are the instance variables. | + | | '''roll_number''' and '''name''' are the '''instance''' variables. |
|- | |- | ||
Line 109: | Line 107: | ||
|- | |- | ||
| 02:55 | | 02:55 | ||
− | |Now Save and run the file.So | + | |Now '''Save''' and '''run''' the file. So press '''Ctrl, S''' and '''Ctrl, F11'''. |
|- | |- | ||
Line 117: | Line 115: | ||
|- | |- | ||
| 03:07 | | 03:07 | ||
− | | ''' I am a | + | | ''' I am a parameterized constructor''' |
− | + | '''0''' | |
+ | ''' null''' | ||
|- | |- | ||
Line 144: | Line 140: | ||
|- | |- | ||
| 03:33 | | 03:33 | ||
− | | This is because in the '''constructor roll_number and name | + | | 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. |
|- | |- | ||
| 03:47 | | 03:47 | ||
− | | Here, | + | | 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" into the constructor. |
|- | |- | ||
Line 163: | Line 159: | ||
|- | |- | ||
| 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 | + | | They have been initialized to '0' and ''null' already once the object is created. |
|- | |- | ||
Line 174: | Line 170: | ||
|- | |- | ||
| 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 | + | '''I am parameterized constructor''' |
'''11''' and | '''11''' and | ||
Line 202: | Line 198: | ||
|- | |- | ||
| 04:58 | | 04:58 | ||
− | | This is because this dot roll_number and this dot 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 | + | | To avoid conflict between '''local''' and '''instance''' variables we use "this" keyword. |
|- | |- | ||
| 05:29 | | 05:29 | ||
− | | Now we will see the use of this | + | | Now, we will see the use of "this" keyword for chaining of '''constructor'''. |
|- | |- | ||
Line 226: | Line 222: | ||
|- | |- | ||
| 05:43 | | 05:43 | ||
− | | | + | | 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 | + | |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 | + | |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 | + | | 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''' |
|- | |- | ||
| 07:54 | | 07:54 | ||
− | | | + | | '''I am a constructor with a single parameter''' |
|- | |- | ||
| 07:57 | | 07:57 | ||
− | | | + | | '''I am Default Constructor''' |
'''11''' and | '''11''' and | ||
Line 281: | Line 277: | ||
|- | |- | ||
| 08:02 | | 08:02 | ||
− | | | + | | Now, I will explain the output. |
|- | |- | ||
Line 289: | Line 285: | ||
|- | |- | ||
| 08:13 | | 08:13 | ||
− | | The constructor here is the no argument '''constructor'''. | + | | The constructor here is the ''no argument'' '''constructor'''. |
|- | |- | ||
Line 297: | Line 293: | ||
|- | |- | ||
| 08:24 | | 08:24 | ||
− | | It encounters the this within brackets 11 statement. | + | | It encounters the '''this''' within brackets '''11''' statement. |
|- | |- | ||
Line 305: | Line 301: | ||
|- | |- | ||
| 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'''. |
|- | |- | ||
Line 313: | Line 309: | ||
|- | |- | ||
| 08:53 | | 08:53 | ||
− | | So this '''constructor''' is executed and we get the output as '''I am | + | | 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''' | + | |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. |
|- | |- | ||
Line 337: | Line 333: | ||
|- | |- | ||
| 09:30 | | 09:30 | ||
− | | So we get output as I am a default | + | | So, we get output as '''I am a default constructor'''. |
|- | |- | ||
Line 353: | Line 349: | ||
|- | |- | ||
| 09:47 | | 09:47 | ||
− | | Make the this statement the last one in the '''constructor'''. | + | | Make the '''this''' statement the last one in the '''constructor'''. |
|- | |- | ||
Line 369: | Line 365: | ||
|- | |- | ||
| 10:07 | | 10:07 | ||
− | | '''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'''. |
|- | |- | ||
Line 385: | Line 381: | ||
|- | |- | ||
| 10:31 | | 10:31 | ||
− | | So in this tutorial, we learnt | + | | So, in this tutorial, we learnt: |
|- | |- | ||
| 10:35 | | 10:35 | ||
− | | | + | |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 | + | | 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]. |
|- | |- | ||
| 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 | + | | 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 | + | | 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 | + | | More information on this mission is available at http://spoken-tutorial.org/NMEICT-Intro. |
|- | |- | ||
Line 465: | Line 461: | ||
|- | |- | ||
| 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