this
keyword is a reference variable in Java.
this
Keyword:
public class ThisDemo
{
int no = 10;
void m1(int no)
{
System.out.println("1. no : "+no); // Prints the value of the local parameter 'no' (method argument)
System.out.println("2. no : "+this.no); //'this.no' refers to the instance variable of the current object
}
public static void main(String[] args)
{
ThisDemo obj = new ThisDemo();
obj.m1(20);
}
}
1. no : 20 2. no : 10
class Student
{
int id;
String name;
// Constructor with parameters having the same name as instance variables
Student(int id, String name)
{
this.id = id; // 'this.id' refers to the instance variable
this.name = name; // 'name' on the right refers to the parameter
}
void display()
{
System.out.println("ID: " + id);
System.out.println("Name: " + name);
}
public static void main(String[] args)
{
Student s1 = new Student(101, "Amit");
s1.display();
}
}
ID: 101 Name: Deepak
public class ThisDemo
{
void showMessage()
{
System.out.println("Hello from showMessage() method");
}
void display()
{
// Calling another method of the same class using 'this'
this.showMessage(); // Optional: can also call showMessage() directly
System.out.println("Inside display() method");
}
public static void main(String[] args)
{
ThisDemo obj = new ThisDemo();
obj.display();
}
}
Hello from showMessage() method Inside display() method
public class ThisDemo
{
// Default constructor
ThisDemo()
{
System.out.println("Default constructor called");
}
// Parameterized constructor
ThisDemo(String name)
{
// Calling default constructor using 'this()'
this(); // Must be the first statement in the constructor
System.out.println("Hello, " + name);
}
public static void main(String[] args)
{
// Creating object using parameterized constructor
ThisDemo obj = new ThisDemo("Deepak");
}
}
Default constructor called Hello, Deepak
public class ThisDemo
{
void display(ThisDemo obj)
{
System.out.println("display() method is called");
}
void call()
{
// Passing current object as argument using 'this'
display(this);
}
public static void main(String[] args)
{
ThisDemo obj = new ThisDemo();
obj.call();
}
}
display() method is called
class A
{
int value = 10;
A()
{
// Passing current instance of A to B's constructor
B b = new B(this);
}
}
class B
{
// Constructor of class B that accepts a reference to class A
B(A obj)
{
System.out.println("Constructor of B is called");
System.out.println("Value from class A: " + obj.value);
}
}
public class ThisDemo
{
public static void main(String[] args)
{
A a = new A();
}
}
Constructor of B is called Value from class A: 10
public class ThisDemo
{
int no;
// Method that sets the no and returns the current class instance
ThisDemo setValue(int no)
{
this.no = no;
return this; // returning current object
}
// Method to display the no
void display()
{
System.out.println("Number: " + no);
}
public static void main(String[] args)
{
// Method chaining using returned instance
ThisDemo obj = new ThisDemo();
obj.setValue(100).display(); // chaining method call
}
}
Number: 100
Your feedback helps us grow! If there's anything we can fix or improve, please let us know.
Weโre here to make our tutorials better based on your thoughts and suggestions.