this keyword in java

Learn this keyword in java with programming uses and examples. – In java programs, this keyword is used in following scenarios in java programs.

  • To refer current class fields (instance variables).
  • To refer current class constructor(implicitly).
  • To refer the current class method.
  • To pass object as an argument to method.
  • To pass object as an argument to constructor.
  • To return object from a method

this keyword refers current class fields

In below example, “this” keyword distinguishes the class variable with parameter variable model. If you write the statement like model = model, it will be difficult to distinguish which variable is assigned to which. Compiler understand if you don’t use this keyword, but for new programmer it is readable. By the way it is a good practice to use this keyword.

class Car {
	String model;

	public Car(String model) {
		this.model = model;
	}
}

this keyword refers current class Constructor

In below example, there is a class Website. If you don’t pass any topic when you create an object as below,

Website site = new Website(); then by default “Java” will be printed.

If you pass the topic as Website site = new Website(“C++”); Then, C++ will be printed.

In the example, there are two constructors Website () and Website (String topic). The Website () calls parameterized constructor using “this” keyword.

class Website {

	public Website() {
		this("Java");
	}

	public Website(String topic) {

		System.out.println(topic);
	}
}

Below call will print Java

public class Sample {

	public static void main(String[] args) {

		Website site = new Website();		
	}
}

Below call will print C++

public class Sample {

	public static void main(String[] args) {

		Website site = new Website("C++");
		
	}
}

this keyword refers current class Methods

This program compare the price of two cars in highestPriceCar(Car c) method. In if condition, to distinguish the incoming car object in parameter and with current car object we have refer the getPrice() method using this keyword.

public void highestPriceCar(Car c) {
		if (this.getPrice() > c.getPrice()) {

		} else {

		}
	}

Example:

class Car {
	private String model;
	private double price;

	public Car(String model, double price) {
		this.model = model;
		this.price = price;
	}

	public double getPrice() {
		return this.price;
	}

	public String getModel() {
		return this.model;
	}

	public void highestPriceCar(Car c) {
		if (this.getPrice() > c.getPrice()) {
			System.out.println(this.getModel() + 
					" is the car with highest price");
		} else {
			System.out.println(c.getModel() + 
					" is the car with highest price");
		}
	}
}

public class Sample {

	public static void main(String[] args) {

		Car m = new Car("Maruti", 100000);
		Car h = new Car("Honda", 300000);
		m.highestPriceCar(h);

	}
}

Output:
Honda is the car with highest price.

Passing current class object as an argument to method with this keyword

In this example, the book class is passing its object to Board class print () method using this keyword to display the title.

In Book class display () method, an object of Board is created, called the method print and passed the Book class object into the method.

class Book {
	String title = "Java";

	public void display() {

		Board d = new Board();
		d.print(this);
	}

	public String title() {
		return title;
	}
}

class Board{
	
	public void print(Book b) {
		System.out.println(b.title());
	}	
}

public class Sample {

	public static void main(String[] args) {

		Book b = new Book();		
		b.display();
	}
}

Passing object as an argument to constructor using this keyword

In this example, the book class is passing its object to Board class constructor using this keyword to display the title.

In Book class display () method, an object of Board is created and the object of Book class is passed to it as an argument. You know that when we create an object the constructor will be called.

class Book {
	String title = "Java";

	public void display() {

		// Pass book object to Board class
		// Constructor
		Board d = new Board(this);
	}

	public String title() {
		return title;
	}
}

class Board {

	public Board(Book b) {
		System.out.println(b.title());
	}
}

public class Sample {

	public static void main(String[] args) {

		Book b = new Book();
		b.display();
	}
}

Returning object from a method using this

The getUpdatedBookInstance() method returns book object using this keyword.

In main(), first book object has been created and printed the book title.

After that getUpdatedBookInstance() method is called in which the previously created object gets modified with C++.

On calling the title() on modified object, it prints C++.

class Book {
	String title = "Java";

	public Book getUpdatedBookInstance() {

		this.title = "C++";
		return this;// return book class object
	}

	public String title() {
		return title;
	}
}

public class Sample {

	public static void main(String[] args) {

		Book b = new Book();
		System.out.println(b.title());

		Book m = b.getUpdatedBookInstance();
		System.out.println(m.title());
	}
}

Output:
Java
C++

Related Posts