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

10 This Keyword in Java

Uploaded by

chintuunagar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

10 This Keyword in Java

Uploaded by

chintuunagar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

QUE: Explain this keyword in Java with a proper example.

ANS:
➢ In Java, this keyword is a reference to the current object within an instance
method or a constructor.
➢ It can be used to refer to the current instance of the class and access its
members, including instance variables and methods.
➢ This keyword can also be used to differentiate between instance variables
and parameters or local variables with the same name.

❖ Ways to use ‘this’ in Java


Following are the ways to use the ‘this’ keyword in Java mentioned below:

(1) Using the ‘this’ keyword to refer to current class instance variables.
(2) Using this() to invoke the current class constructor
(3) Using ‘this’ keyword to return the current class instance
(4) Using ‘this’ keyword as the method parameter
(5) Using ‘this’ keyword to invoke the current class method
(6) Using ‘this’ keyword as an argument in the constructor call
Lets understand each way to use this keyword.

(1) Using the ‘this’ keyword to refer to current class instance variables

➢ Inside an instance method or constructor, this can be used to refer to


instance variables of the current class.

➢ This is particularly useful when there is a naming conflict between instance


variables and method parameters or local variables.

➢ When there is a naming conflict between instance variables and method


parameters or local variables, this helps distinguish between them.

➢ It ensures that the correct variable (instance variable) is accessed or


modified.

➢ Example:

class Rectangle
{
double length;
double breadth;

Rectangle(double length, double breadth)


{
this.length = length; //using this to point out above variables
this.breadth = breadth;
}

double calculateArea()
{
return length*breadth;
}
}
class this_keyword
{
public static void main(String[] args) {
Rectangle myrec = new Rectangle(3,4);
System.out.println("Area of Rectangle: " +(myrec.calculateArea()));
}
}

(2) Using this() to invoke the current class constructor

➢ Using this() to invoke the current class constructor is a mechanism in


some programming languages that allows a constructor to call another
constructor within the same class.

➢ This can be helpful when you have multiple constructors in a class with
different parameters, and you want to avoid duplicating initialization
code.

class Rectangle
{
double length;
double breadth;

Rectangle()
{
System.out.println("Non-argument constructor called!");
}

Rectangle(double length, double breadth)


{
this(); //gives call to current class constructor
this.length = length; //using this to point out above variables
this.breadth = breadth;
}

double calculateArea()
{
return length*breadth;
}
}
class this_keyword
{
public static void main(String[] args) {
Rectangle myrec = new Rectangle(3,4);
System.out.println("Area of Rectangle: " +
(myrec.calculateArea()));
}
}
(3) Using ‘this’ keyword to return the current class instance
➢ Using the 'this' keyword to return the current class instance refers to a
programming technique in object-oriented languages where 'this' is used
within a method to explicitly return the instance of the class on which the
method is invoked.

➢ This allows for chaining method calls on the same instance, which can
enhance code readability and conciseness.

➢ By returning 'this', the method indicates that it has completed its operation
and returns the current instance, enabling method chaining without needing
to explicitly store and return the instance manually.

class Rectangle
{
double length;
double breadth;

Rectangle(double l, double b)
{
length = l;
breadth = b;
}
Rectangle meth()
{
Rectangle r = new Rectangle(length*10, breadth*10);
return this;
}
}
class this_keyword
{
public static void main(String[] args) {
Rectangle myrec = new Rectangle(3,4);
Rectangle ob;
System.out.println("The original length and breadth: "+myrec.length +"
and " +myrec.breadth);
ob = myrec.meth();
System.out.println("The modified length and breadth: "+ob.length +"
and " +ob.breadth);
}
}
(4) Using ‘this’ keyword as the method parameter
➢ Using the 'this' keyword as the method parameter is a concept in object-
oriented programming where 'this' is passed as an argument to a method
within the same class.

➢ This allows the method to access or modify the instance variables and
methods of the current object.

➢ By passing 'this' as a parameter, the method gains access to the current


object's state without needing to explicitly reference it, promoting code
clarity and simplifying method invocations within the class.

class Rectangle
{
double length;
double breadth;

Rectangle(double l, double b)
{
length = l;
breadth = b;
}

void calculateArea(Rectangle obj)


{
System.out.println("Area of the Rectangle: " +obj.length*obj.breadth);
}

void callmethod()
{
calculateArea(this);
}
}
class this_keyword
{
public static void main(String[] args) {
Rectangle myrec = new Rectangle(3,4);
myrec.callmethod();
Rectangle myrec2 = new Rectangle(7,4);
myrec2.callmethod();
}
}
(5) Using ‘this’ keyword to invoke the current class method
➢ Using the 'this' keyword to invoke the current class method is a technique
in object-oriented programming where 'this' is employed within a method
to call another method within the same class.

➢ By using 'this.methodName()', the method references itself and invokes


another method belonging to the same class.

➢ This approach is particularly useful for method chaining or for invoking


overloaded methods within the class without ambiguity.

➢ It ensures that the method being called is explicitly from the current
instance, enhancing code readability and reducing potential confusion.

class Rectangle
{
double length;
double breadth;

Rectangle(double length, double breadth)


{
this.length = length; //using this to point out above variables
this.breadth = breadth;
}

void calculateArea()
{
double area = length*breadth;
System.out.println("Area of the Rectangle: " +area);
}

void callmethod()
{
this.calculateArea(); //calls current class method
}
}
class this_keyword
{
public static void main(String[] args) {
Rectangle myrec = new Rectangle(3,4);
myrec.calculateArea();
Rectangle myrec2 = new Rectangle(7,4);
myrec2.callmethod();
}
}

(6) Using ‘this’ keyword as an argument in the constructor call


➢ Using the 'this' keyword as an argument in the constructor call is a practice
in object-oriented programming where 'this' is employed within a
constructor to invoke another constructor of the same class.

➢ By passing 'this' as an argument, the constructor delegates the


responsibility of object initialization to another constructor within the same
class.

➢ This technique is particularly useful for code reuse and reducing


redundancy in constructor implementations.

➢ It ensures that common initialization logic is centralized in one constructor,


enhancing code maintainability and readability.

class Rectangle
{
double length;
double breadth;

Rectangle()
{
Rectangle r = new Rectangle(this);

Rectangle(Rectangle rt)
{
rt.length = 10; //using this to point out above variables
rt.breadth = 12;
}

double calculateArea()
{
return length*breadth;
}
}
class this_keyword
{
public static void main(String[] args) {
double area;
Rectangle myrec = new Rectangle();
System.out.println("Area of Rectangle: " +(myrec.calculateArea()));
}
}

You might also like