Java/C2/Using-this-keyword/English-timed
From Script | Spoken-Tutorial
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