0% found this document useful (0 votes)
12 views

This Pointer

This document discusses the usage of the "this" keyword in Java. It explains that this is a reference variable that refers to the current object. It can be used to refer to current class instance variables, invoke current class methods, invoke current class constructors, pass the current class instance as an argument, and return the current class instance from methods. This helps resolve issues like namespace collisions between local variables and instance variables with the same name. The document provides examples of how to use this to invoke methods, constructors, and resolve namespace collisions.

Uploaded by

Ann Maria Jose
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

This Pointer

This document discusses the usage of the "this" keyword in Java. It explains that this is a reference variable that refers to the current object. It can be used to refer to current class instance variables, invoke current class methods, invoke current class constructors, pass the current class instance as an argument, and return the current class instance from methods. This helps resolve issues like namespace collisions between local variables and instance variables with the same name. The document provides examples of how to use this to invoke methods, constructors, and resolve namespace collisions.

Uploaded by

Ann Maria Jose
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

this pointer

• In java, this is a reference variable that refers


to the current object.
Usage of java this keyword
• this can be used to refer current class instance
variable.
• this can be used to invoke current class method
(implicitly)
• this() can be used to invoke current class constructor.
• this can be passed as an argument in the method call.
• this can be passed as argument in the constructor
call.
• this can be used to return the current class instance
from the method.
usage
1.Resolves namespace collision
• this: to refer current class instance variable
• The this keyword can be used to refer current
class instance variable. If there is ambiguity
between the instance variables and
parameters, this keyword resolves the
problem of ambiguity(namespace collision).
• If the local variable and instance variable both
having same name,the local variable hides the
instance variable. This is known as namespace
collision.
How it resolves!!!
namespace collision Resolves using this
Where this not required
• If local variables(formal arguments) and instance variables are different,
there is no need to use this keyword like in the following program
2. this: to invoke current class method

• You may invoke the method of the current class by using the
this keyword.
• If you don't use the this keyword, compiler automatically
adds this keyword while invoking the method. Let's see the
example
Example
3) this() : to invoke current class constructor

• The this() constructor call can be used to invoke the current class
constructor.
• It is used to reuse the constructor. In other words, it is used for
constructor chaining.
Real usage of this() constructor call

• The this() constructor call should be used to reuse


the constructor from the constructor.
• It maintains the chain between the constructors i.e.
it is used for constructor chaining.
• Call to this() must be the first statement in
constructor.
• Let's see the example given that displays the actual
use of this keyword.
Example-reusing constructor
finalize()
• finalize() is called by the garbage collector on an object
when garbage collection determines that there are no
more references to the object. A subclass overrides
the finalize method to dispose of system resources or
to perform other cleanup.
• It belongs as java.lang.Object.finalize() method
• protected void finalize()
{
…..
…..
}

You might also like