Java/C2/Using-this-keyword/English

From Script | Spoken-Tutorial
Revision as of 15:47, 27 November 2012 by Chandrika (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Title of script: Using this keyword

Author: Arya Ratish

Keywords: video tutorial, this, constructor


Visual Cue
Narration
Slide 1

Opening slide

Welcome to the Spoken Tutorial on using this keyword in java.
Slide 2

Learning Objectives

In this tutorial we will learn

- About use of this keyword

- To use this keyword with fields

-To use this keyword for chaining of constructors.



Slide 3

System Requirements

Here we are using
  • Ubuntu version 11.10
  • Java Development Environment jdk 1.6
  • Eclipse 3.7


Slide 4

Prerequisites

To follow this tutorial you must know

how to create a default constructor in java using eclipse.


You must also know how to create a parameterized constructor in java.


If not, for relevant tutorials please visit our website which is as shown,

(http://www.spoken-tutorial.org)

Slide 5

Use of this keyword

Within a constructor, this is a reference to the current object.


We can refer any member of the current object within a constructor using this.

Slide 6


Using this with fields

this keyword helps us to avoid name conflicts.


We can see such an example here.

Open Eclipse For that let us open Eclipse.


Open the Student class we had created in the earlier tutorial.


Comment the default constructor and the constructor with 1 parameter.


Also comment the code for creating the first two objects.


Now, notice the parameterized constructor.


the_roll_number and the_name are arguments passed in the constructor.


roll_number and name are the instance variables.


Now, let me change the arguments to roll_number and name itself.


So inside the constructor we have:

roll_number equal to roll_number and name equal to name.


Save and run the file.

Highlight the output. Now, let us see the output.


We get the output as follows:


I am Parameterized Constructor
0
null

We can see 2 warnings in the code.


The assignment to variable roll_number has no effect.

The assignment to variable name has no effect.


This is because in the constructor roll_number and name are local variables.


Local variables are variables accessible within a method or block.


Here, roll_number and name will be initialized to 11 and Raju.


Once they come out of the constructor, it is not accessible.


Then the only roll_number and name we know are the instance variables.


They have been initialized to 0 and null already once the object is created.


So we got the output as 0 and null.

Type this.roll_number=roll_number

this.name=name

Now, let us make a small change inside the constructor.


Type this dot roll_number equal to roll_number.


Next line type this dot name equal to name.


Now save and run the file.

Highlight the output. We get the output as


I am Parameterized Constructor
11
Raju

This is because this dot roll_number and this dot name refers to the instance variables roll_number and name.


roll_number and name refers to the arguments passed in the method.


To avoid confliction between local and instance variables we use this keyword.

Slide 7


Using this for chaining of constructors

We can use this keyword inside a constructor to call another one.


The constructors must be in the same class.


This is called explicit constructor invocation.

Switch to Eclipse


Point to Student class

Let us come back to the Student class which we created.
Remove the comments.


Comment the part to assign the instance variables in the first two constructors.


Comment the part which creates the second and third objects.

Point to default constructor. Let us first come to the first constructor with no parameters.


After the curly brackets type this within brackets 11 semicolon.


Press Enter.

Point to the constructor with 1 argument. Inside the second constructor type this within brackets 11 comma Raju semicolon.


Press Enter.


Save and Run the file.

Highlight the output We get the output as


I am Parameterized Constructor
I am a constructor with a single parameter
I am Default Constructor

11

Raju

Highlight each constructor as you explain Now, I will explain the output.


When the object is created, the respective constructor gets called.


The constructor here is the no argument constructor.


The control comes to the first line in the constructor.


It encounters the this within brackets 11 statement.


Hence it calls the constructor that accepts 1 integer argument.


Then the control comes to this within brackets 11 comma Raju.


Hence it calls the constructor that accepts 1 integer and 1 String argument.


So this constructor is executed and we get the output as I am Parameterized Constructor.


The instance variables are initialized here to 11 and Raju.


Now, the control goes back to the calling constructor.


So the second constructor is executed.


We get the output as I am constructor with a single parameter.


Then, the control goes to the first constructor and executes it.


So we get output as I am default constructor.


Then studentDetail method is executed.


So, we get 11 and Raju.

Hover the mouse over the error symbol. Now, let us make a small change.


Make the this statement the last one in the constructor.


We get compiler error.


Hover the mouse over the error symbol.


We get the error as:


Constructor call must be the first statement in the constructor.

So we must make it the first line of the constructor.
Let us make it the first line of the constructor.


We can see that the error has gone.

Slide 6

Summary

So in this tutorial, we have learnt

To use this keyword with fields.

To use this keyword for chaining constructors

How this keyword should be used within a constructor.



Slide 7

Assignment

For self assessment, in the Employee class created earlier:
  • Create a constructor with two parameters
  • Use this keyword to initialize the instance variables .
  • Also create a constructor with 1 and no parameters.
  • Try chaining the constructors using this as explained in the tutorial.


Slide 8

About Slide

To know more about the Spoken Tutorial Project
  • If you do not have good bandwidth, you can download and watch it


Slide 9

About Slide


The Spoken Tutorial Project Team
  • Conducts workshops using spoken tutorials
  • Gives certificates for those who pass an online test
  • For more details, please write to contact@spoken-tutorial.org


Slide 10

Acknowledgment

Spoken Tutorial Project is a part of the Talk to a Teacher project
  • It is supported by the National Mission on Education through ICT, MHRD, Government of India
  • More information on this Mission is available at


We have come to the end of this tutorial.

Thanks for joining.

This is Arya Ratish signing off.

Jai Hind.

Contributors and Content Editors

Chandrika