The this keyword in Java is a reference variable that refers to the current class/object. It is used to eliminate ambiguity and to access instance variables and methods of the current object within its class.
To return current class object by using 'this' keyword thn we have to use the followng
Syntax
return this;
this keyword can be used to return the current object from a method.
class Car {
private String brand;
private String model;
public Car(String brand, String model) {
this.brand = brand;
this.model = model;
}
public Car getCar() {
return this; // Returns the current object
}
public void displayDetails() {
System.out.println("Brand: " + brand);
System.out.println("Model: " + model);
}
}
public class Main {
public static void main(String[] args) {
Car car = new Car("Toyota", "Camry");
car.getCar().displayDetails(); // Returns and displays the current object's details
}
}
Brand: Toyota
Model: Camry
If we want to refer current class constructor by using 'this' keyword then we have to use the following
syntax:
this([param_List]);
this can be used to call another constructor in the same class (constructor chaining).
xxxxxxxxxx
class A
{
A()
{
this(10);
System.out.println("A-0-arg-con");
}
A(int i)
{
this(22.22f);
System.out.println("A-int-param-con");
}
A(float f)
{
this(33.3333);
System.out.println("A-float-param-con");
}
A(double d)
{
System.out.println("A-double-param-con");
}
}
class Test {
public static void main(String args[])
{
A a = new A();
}
}
A-double-param-con
A-float-param-con
A-int-param-con
A-0-arg-con
Note:
`this` (or) `super` but not both, must be the first statement in class constructor
, if we are not writing anyone of this then compiler will always place the `super()`
Constructor Overloading:
In the above program we have provided more than one constructor with the same name and with the different parameter list, this process is called as "Constructor Overloading".
Constructor Chaining:
In the above program, we have called all the current class constructors by using 'this'
keyword in chain fashion, this process is called as "Constructor Chaining".
Note:
If we want to access current class constructor by using 'this' keyword then the respective 'this' statement must be provided as first statement, if we have not provided 'this' statement as first statement then compiler will rise an error like 'call to this must be first statement in constructor'
If we want to refer current class method by using 'this' keyword then we have to use the following.
Syntax
this.method_Name([param_List]);
Note:
To access current class methods, it is not required to use any reference variable and any keyword including 'this', directly we can access.
xxxxxxxxxx
class A {
void m1() {
System.out.println("m1-A");
}
void m2() {
System.out.println("m2-A");
m1();
this.m1();
}
}
public class Test {
public static void main(String args[]) {
A a = new A();
a.m2();
}
}
m2-A
m1-A
m1-A
Passing the current object as a parameter to another method is a common practice in Java. It allows one object to be aware of another object's state or behavior. This is often used in design patterns and methods that need to operate on the object itself or share information with other objects.
xxxxxxxxxx
class Car {
private String brand;
private String model;
// Constructor
public Car(String brand, String model) {
this.brand = brand;
this.model = model;
}
// Method to display car details
public void displayDetails() {
System.out.println("Brand: " + brand);
System.out.println("Model: " + model);
}
// Method to send the car to the mechanic for inspection
public void sendForInspection(Mechanic mechanic) {
mechanic.inspectCar(this); // Passing the current object to the mechanic
}
}
class Mechanic {
private String name;
// Constructor
public Mechanic(String name) {
this.name = name;
}
// Method to inspect the car
public void inspectCar(Car car) {
System.out.println("Mechanic " + name + " is inspecting the car:");
car.displayDetails(); // Accessing the car's details using the passed object
}
}
public class PlanetLearning {
public static void main(String[] args) {
// Creating a Car object
Car myCar = new Car("Toyota", "Camry");
// Creating a Mechanic object
Mechanic myMechanic = new Mechanic("John");
// Sending the car for inspection
myCar.sendForInspection(myMechanic);
}
}
Mechanic John is inspecting the car:
Brand: Toyota
Model: Camry
When local variables and instance variables have the same name, this is used to distinguish between them.
xxxxxxxxxx
class Car
{
private String brand;
private String model;
int i=10;
int j=20;
public Car(String brand, String model)
{
this.brand = brand;
// 'this.brand' refers to the instance variable, 'brand' refers to the parameter
this.model = model;
}
public Car(int i,int j)
{
System.out.println(i+" "+j); // local variables
System.out.println(this.i+" "+this.j); // instance variables
}
}
public class Test {
public static void main(String args[])
{
Car a = new Car(30,40);
}
}
30 40
10 20
Note:
In Java applications, if we provide same set of variables at local and at class level and if we access that variables then JVM will give first priority for local variables, if local variables are not available then JVM will search for that variables at class level, even at class level also if that variables are not available then JVM will search at super class level.At all the above locations, if the specified variables are not available then compiler will rise an error.