Java this keyword - Example

The 'this' keyword in Java is a keyword that can be used inside a class method or constructor. The 'this' keyword works as the reference to the current object, whose method or constructor is being invoked. You can use the 'this' keyword to reference any member of the current object inside an instance method or constructor.

Following are the ways to use the 'this' keyword in java:

1. Using '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 a method parameter

5. Using 'this' keyword to invoke the current class method

6. Using 'this' keyword as an argument in the constructor call


Using 'this' keyword to refer to current class instance variables

//Java code for using 'this' keyword to
//refer current class instance variables
public class KNFDemo {

int a;
int b;

// Parameterized constructor
KNFDemo(int a, int b) {
this.a = a;
this.b = b;
}

void display() {
// Displaying value of variables a and b
System.out.println("a = " + a + " b = " + b);
}

public static void main(String[] args) {
KNFDemo object = new KNFDemo(50, 70);
object.display();
}
}

Console output:

a = 50  b = 70



Using this() to invoke the current class constructor

// Java code for using this() to
// invoke current class constructor
public class KNFDemo {

int a;
int b;

// Default constructor
KNFDemo() {
this(50, 60);
System.out.println("Inside default constructor \n");
}

// Parameterized constructor
KNFDemo(int a, int b) {
this.a = a;
this.b = b;
System.out.println("Inside parameterized constructor");
}

public static void main(String[] args) {
KNFDemo object = new KNFDemo();
}
}

Console output:

Inside parameterized constructor

Inside  default constructor 



Using 'this' keyword to return the current class instance

//Java code for using 'this' keyword
//to return the current class instance
public class KNFDemo {

int a;
int b;

// Default constructor
KNFDemo() {
a = 60;
b = 80;
}

// Method that returns current class instance
KNFDemo get() {
return this;
}

// Displaying value of variables a and b
void display() {
System.out.println("a = " + a + " b = " + b);
}

public static void main(String[] args) {
KNFDemo object = new KNFDemo();
object.get().display();
}
}

Console output:

a = 60  b = 80



Using 'this' keyword as a method parameter

// Java code for using 'this'  keyword as a method parameter
public class KNFDemo {

int a;
int b;

// Default constructor
KNFDemo() {
a = 80;
b = 100;
}

// Method that receives 'this' keyword as parameter
void display(KNFDemo obj) {
System.out.println("a = " + a + " b = " + b);
}

// Method that returns current class instance
void get() {
display(this);
}

public static void main(String[] args) {
KNFDemo object = new KNFDemo();
object.get();
}
}

Console output:

a = 80  b = 100



Using 'this' keyword to invoke the current class method

// Java code for using this to invoke current
// class method
public class KNFDemo {
void display() {
// calling fuction show()
this.show();
System.out.println("Inside display function");
}
void show() {
System.out.println("Inside show funcion");
}
public static void main(String args[]) {
KNFDemo obj = new KNFDemo();
obj.display();
}
}

Console output:

Inside show funcion

Inside display function



Using 'this' keyword as an argument in the constructor call

// Java code for using this as an argument in constructor
// call
// KNFDemoA with object of KNFDemoA as its data member
public class KNFDemoA {
KNFDemoC obj;
// Parameterized constructor with object of KNFDemoC
// as a parameter
KNFDemoA(KNFDemoC obj) {
this.obj = obj;
// calling display method of class KNFDemoC
obj.display();
}
public static void main(String[] args) {
KNFDemoC obj = new KNFDemoC();
}
}
class KNFDemoC {
int x = 5;
// Default Constructor that create a object of KNFDemoA
// with passing this as an argument in the
// constructor
KNFDemoC() {
KNFDemoA obj = new KNFDemoA(this);
}
// method to show value of x
void display() {
System.out.println("Value of x in KNFDemoC : " + x);
}

}

Console output:

Value of x in KNFDemoC : 5

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Spring Boot + Mockito simple application with 100% code coverage

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete