this pointer .17
TRANSCRIPT
9CM604.171
Usage of this pointer
9CM604.172
Objectives
On completion of this period, you would be able to know, the working of :
• ‘this’ pointer• The usage of this pointer
9CM604.17
Recap• In the previous class, we have discussed
• Concepts of constructor
• How constructors are created ?
• Types of constructors
• Default
• Explicit
• Parameterized
• Overloaded
9CM604.174
• Some times a method will need to refer to the object that invoked it
• To allow this, Java defines the this keyword
• i.e. ‘this’ keyword is used to rtefer to the current object
• This can be used inside any method to refer to the current object
• i.e. this is always a reference to the object on which the method was invoked
• You can use this anywhere a reference to an object of the current class type is permitted
this Pointer
9CM604.175
• Broadly ‘this’ keyword can be used in the following ways
• Instance variable hiding
• To define constructors
• To pass the current object as a parameter
Usage Of this Keyword
9CM604.176
• It is illegal in Java to declare two local variables with the same name inside the same enclosing scopes
• You can have local variable, including formal parameters to methods, which overlap with the names of the class instance variables
• When a local variable has the same name as an instance variable, the local variable hides the instance variable
Instance Variables Hiding
9CM604.177
• The following example shows the formal parameter hiding the instance variables
class Point{int x,y;Point(int x, int y){
this.x = x;this.y = y;
}}
Instance Variables Hiding contd..
9CM604.178
• In the above example, x and y are instance variables
• In the constructor, the names of formal parameters are also x and y
• These variables hide the instance variables
• To access the instance variable use ‘this’ pointer
• In the constructor
• this.x refers to instance variable
• Simply x refers to the formal parameter
Instance Variables Hiding contd..
9CM604.179
• To call the constructor of the current class inside the current class ‘this’ keyword is used
• The following example shows how to do itclass Point{
int x, y;
Point(int x, int y){
this.x = x;
this.y = y;
}
Point ( ){
this(0,0);
}
}
Usage of ‘this’ In Defining Constructor
‘this’ keyword is used to call the current class’s constructor
9CM604.1710
• In the second constructor the statement
• this(0,0);
• Calls the first constructor by passing 0, 0 as parameters
Usage of ‘this’ In Defining Constructor contd...
9CM604.1711
• The third way to use ‘this’ keyword is to pass the current object as parameter through an instance method
• The general form of it looks like
• methodName (this);
Usage of ‘this’ In Parameter Passing contd...
9CM604.1712
Summary
• In this class we have discussed• ‘this’ pointer• Usage of ‘this’ pointer
9CM604.17
1. this is a
a. Variable
b. Instance variable
c. A pointer
d. None
13
Quiz
9CM604.17
Quiz contd...
2. this access
a. Variable
b. Instance variable
c. Constant
d. Static variable
9CM604.17
1. Explain the use of this pointer with an example program
2. How we can hide the instance variables using this pointer?
15
Frequently Asked Questions
9CM604.17
1. Write a class that implements this pointer
2. Create a Student class that calls its constructor using ‘this’ keyword
16
Assignment