📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Check out All 50 Java Keywords with Examples
Let's discuss each of these usages of this keyword with an example.
1. this keyword can be used to refer to a current class instance variable
The this keyword can be used to refer current class instance variable.Understanding the problem without this keyword
public class User {
private int id;
private String firstName;
private String lastName;
private int age;
public User(int id, String firstName, String lastName, int age) {
id = id;
firstName = firstName;
lastName = lastName;
age = age;
}
@Override
public String toString() {
return "User [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", age=" + age + "]";
}
public static void main(String[] args) {
User user = new User(1, "Ramesh", "Fadatare", 28);
System.out.println(user.toString());
}
}
User [id=0, firstName=null, lastName=null, age=0]
The solution to the above problem with this keyword
/**
*
* @author Ramesh Fadatare
*
*/
public class UserWithThisKeyword {
private int id;
private String firstName;
private String lastName;
private int age;
public UserWithThisKeyword(int id, String firstName, String lastName, int age) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
@Override
public String toString() {
return "User [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", age=" + age + "]";
}
public static void main(String[] args) {
UserWithThisKeyword user = new UserWithThisKeyword(1, "Ramesh", "Fadatare", 28);
System.out.println(user.toString());
}
}
User [id=1, firstName=Ramesh, lastName=Fadatare, age=28]
2. this keyword can be used to invoke current class method (implicitly)
public class ThisKeywordWithMethod {
public static void main(String[] args) {
A a = new A();
a.print();
}
}
class A {
public void display() {
System.out.println("Inside display method");
}
public void print() {
this.display();
System.out.println("Inside print method");
}
}
Inside display method
Inside print method
3. this() can be used to invoke the current class constructor
class MyClass {
int a;
int b;
// initialize a and b individually
MyClass(int i, int j) {
a = i;
b = j;
}
// initialize a and b to the same value
MyClass(int i) {
a = i;
b = i;
}
// give a and b default values of 0
MyClass() {
a = 0;
b = 0;
}
}
package com.javaguides.corejava.keywords.thiskeyword;
public class ThisKeywordWithConstructor {
public static void main(String[] args) {
MyClass myClass = new MyClass();
MyClass myClass1 = new MyClass(10);
MyClass myClass2 = new MyClass(10, 20);
}
}
class MyClass {
int a;
int b;
// initialize a and b individually
MyClass(int i, int j) {
a = i;
b = j;
}
// initialize a and b to the same value
MyClass(int i) {
this(i, i); // invokes MyClass(i, i)
}
// give a and b default values of 0
MyClass() {
this(0); // invokes MyClass(0)
}
}
4. this keyword can be passed as an argument in the method call
public void print() {
printStudent(this);
}
package com.javaguides.corejava.keywords.thiskeyword;
/**
*
* @author Ramesh Fadatare
*
*/
public class ArgumentInMethodThis {
public static void main(String[] args) {
Student student = new Student("Ramesh", "STU100", "Physics");
student.print();
}
}
class Student {
private String name;
private String rollNo;
private String course;
public Student(String name, String rollNo, String course) {
super();
this.name = name;
this.rollNo = rollNo;
this.course = course;
}
public void print() {
printStudent(this);
}
private void printStudent(Student student) {
System.out.println(student.name);
System.out.println(student.course);
System.out.println(student.rollNo);
}
}
Ramesh
Physics
STU100
5. this keyword can be passed as an argument in the constructor call
public class ArgumentInConstructorThis {
public static void main(String[] args) {
ClassB classB = new ClassB(10, "Demo");
}
}
class ClassA {
ClassB classB;
public ClassA(ClassB classB) {
this.classB = classB;
printClassB();
}
public void printClassB() {
System.out.println(this.classB.getId());
System.out.println(this.classB.getName());
}
}
class ClassB {
private int id;
private String name;
public ClassB(int id, String name) {
this.id = id;
this.name = name;
new ClassA(this);
}
public int getId() {
return id;
}
public String getName() {
return name;
}
}
10
Demo
6. this keyword can be used to return the current class instance from the method
package com.javaguides.corejava.keywords.thiskeyword;
public class ThisReturnExample {
public static void main(String[] args) {
DemoClass demoClass = new DemoClass();
demoClass.test().print();
}
}
class DemoClass {
public DemoClass test() {
return this;
}
public void print() {
System.out.println("Inside Demo Class");
}
}
Inside Demo Class
Check out All 50 Java Keywords with Examples
Comments
Post a Comment
Leave Comment