This pointer .17

Download This pointer .17

Post on 20-Jun-2015




0 download

Embed Size (px)


<ul><li> 1. Usage of this pointer9CM604.171</li></ul> <p> 2. ObjectivesOn completion of this period, you would be able to know, the working of : this pointer The usage of this pointer 9CM604.17 2 3. Recap In the previous class, we have discussed Concepts of constructor How constructors are created ? Types of constructors Default Explicit Parameterized Overloaded 9CM604.17 4. this Pointer Some times a method will need to refer to the object thatinvoked 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 currentobject i.e. this is always a reference to the object on which the methodwas invoked You can use this anywhere a reference to an object of thecurrent class type is permitted9CM604.174 5. Usage Of this Keyword Broadly this keyword can be used in the following ways Instance variable hiding To define constructors To pass the current object as a parameter9CM604.17 5 6. Instance Variables Hiding It is illegal in Java to declare two local variables with thesame name inside the same enclosing scopes You can have local variable, including formal parameters tomethods, which overlap with the names of the class instancevariables When a local variable has the same name as an instancevariable, the local variable hides the instance variable9CM604.176 7. Instance Variables Hidingcontd.. The following example shows the formal parameter hidingthe instance variables class Point{ int x,y; Point(int x, int y){this.x = x;this.y = y; } }9CM604.177 8. Instance Variables Hidingcontd.. In the above example, x and y are instance variables In the constructor, the names of formal parameters are also xand 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 9CM604.178 9. Usage of this In Defining Constructor To call the constructor of the current class inside the currentclass 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;this keyword is used to call } the current classs constructor Point ( ){ this(0,0); }} 9CM604.17 9 10. Usage of this In Defining Constructor contd... In the second constructor the statement this(0,0); Calls the first constructor by passing 0, 0 as parameters9CM604.1710 11. Usage of this In Parameter Passing contd... The third way to use this keyword is to pass the currentobject as parameter through an instance method The general form of it looks like methodName (this); 9CM604.17 11 12. Summary In this class we have discussed this pointer Usage of this pointer 9CM604.17 12 13. Quiz1. this is a a. Variable b. Instance variable c. A pointer d. None9CM604.1713 14. Quizcontd...2. this access a. Variable b. Instance variable c. Constant d. Static variable9CM604.17 15. Frequently Asked Questions1. Explain the use of this pointer with an example program2. How we can hide the instance variables using this pointer? 9CM604.17 15 16. Assignment1. Write a class that implements this pointer2. Create a Student class that calls its constructor using this keyword9CM604.17 16</p>