this keyword
- this keyword is reference variable or instance variable which holds the address of current object.
It is used inside methods and constructors which refer to the current object
Example:
class HelloWorld {
public static void main(String[] args) {
MyClass obj = new MyClass("Hello, Ashish...");
obj.display(); // output : Hello, Ashish...
}
}
class MyClass {
String message; //instance variable
HelloWorld(String message) {
this.message = message; // here 'this' refers to current object's variable
}
void display() {
System.out.println(this.message); // here 'this' refers to current object
}
}- It helps to distinguish between class variables and local variables when they have the same name.
Example:
class HelloWorld {
public static void main(String[] args) {
MyClass obj = new MyClass();
obj.display();
//output :
// Local Variable: Hello from method!
//Instance Variable: Hello from class!
}
}
class MyClass {
String message = "Instance Variable: Hello from class!"; // instance variable
void display() {
String message = "Local Variable: Hello from method!"; // local variable
System.out.println(message); // refers to local variable
System.out.println(this.message); // refers to instance variable
//So here same variable name is used for both local and instance variables, 'this' helps to distinguish them.
}
}- In constructor chaining, when multiple constructors are present in a class, we can call one constructor from another using the this keyword
Example:
class HelloWorld {
public static void main(String[] args) {
MyClass obj = new MyClass(); // Creating object will call the no-argument constructor first
}
}
class MyClass {
// Constructor 1 (no-argument constructor)
MyClass() {
// 'this' keyword must be the first statement in the constructor
this("Hello from parameterized constructor", 100); // this() is used to call constructor-2 of the same class
System.out.println("No-arg constructor called");
}
// Constructor 2 (parameterized constructor)
MyClass(String message, int number) {
System.out.println("Parameterized constructor called");
}
}- Using this to call an instance method.
Example:
class HelloWorld {
public static void main(String[] args) {
MyClass obj = new MyClass();
obj.show(); //output : Instance method called
}
}
class MyClass {
void display() {
// instance method
System.out.println("Instance method called");
}
void show() {
// this.display() calls the instance method of current object
this.display();
}
}- It cannot be used in a static context as it is associated with the object. Because static methods belong to the class, not objects.
Example:
class HelloWorld {
public static void main(String[] args) {
show();
}
static void show() {
// this cannot be used here because show() method is static
System.out.println(this); // ❌ Compilation error - non-static variable this cannot be referenced from a static context
}
}- this() should be the first statement in a constructor.
Example:
class HelloWorld {
public static void main(String[] args) {
MyClass obj = new MyClass();
}
}
class MyClass {
MyClass() {
// this() must be the first statement in constructor
this(10);
System.out.println("No-argument constructor");
}
MyClass(int x) {
System.out.println("Parameterized constructor: " + x);
}
}See you in next page....🙂