this keyword in Java
this keyword refers to the current object in a method or constructor. The most common use of the this keyword is to eliminate the confusion between class attributes and parameters. Here, parameters means constructor parameter or method parameter.
1. Refer as current class instance2. To call current class method3. To invoke current class constructor4. To pass current object as an argument of method5. To pass argument in constructor call6. To return current state of object
The this keyword can be used to refer current class instance variable. If data member initialized using constructor or method, which has same parameter name. Java provide first preference to its local variable inside the block. If variable(parameter) and data member name are same inside block, Java compiler consider it as single variable. To avoid this ambiguity between the instance variables and variable(parameters), data member use this keyword.
Example
 1 package com.examples;
 2 
 3 public class Example {
 4 
 5 	private String name;
 6 	private double amount;
 7 
 8 	Example() {
 9 		// 3. To invoke current class constructor
 10 		this("Default", 0);
 11 	}
 12 
 13 	Example(Example ins) {
 14 		this.name = ins.name;
 15 		this.amount = ins.amount;
 16 	}
 17 
 18 	Example(String name, double amount) {
 19 		// 1. Here, this referred as current instance of class
 20 		this.name = name;
 21 		this.amount = amount;
 22 	}
 23 
 24 	public Example createCopy(Example ins) {
 25 		// 5. To pass argument in constructor call
 26 		Example e = new Example(this);
 27 		System.out.println(e.amount);
 28 		// 6. To return current state of object
 29 		return this;
 30 	}
 31 
 32 	public double getPrice() {
 33 		// 2. To call current class method
 34 		// 4. To pass current object as an argument of method
 35 		return this.calculate(this);
 36 	}
 37 
 38 	private double calculate(Example ins) {
 39 		// calculate price
 40 		return ins.amount;
 41 	}
 42 }
In the above example, comment is provided with usage number. how to use this keyword in java program.

Access object properties using this keyword

Access object properties using this keyword
 1 package com.java;
 2 
 3 public class Example {
 4 
 5     private String message;
 6     private Integer value;
 7 
 8     public static void main(String[] args) {
 9         Example exp = new Example();
 10         exp.message = "hello world !";
 11         exp.value = 1000;
 12 
 13         exp.print();
 14     }
 15 
 16     public void print() {
 17         System.out.println("Message : " + this.message);
 18         System.out.println("Value : " + this.value);
 19     }
 20 }
In the above example, a class is created with two properties of type string and integer. An instance of class is created and initialized an instance property with respective values. An instance method print() called that access the instance properties using this keyword. It prints a values of the respective property and print.
Output
 1 Message : hello world !
 2 Value : 1000

Example 2

Example 2
 1 package com.java;
 2 
 3 import java.util.Objects;
 4 
 5 class Model {
 6     private final String name;
 7     private final Long salary;
 8 
 9     Model(String name, Long salary) {
 10         this.name = name;
 11         this.salary = salary;
 12     }
 13     public String getName() {
 14         return name;
 15     }
 16 
 17     public Long getSalary() {
 18         return salary;
 19     }
 20 
 21     // compare objects properties using this keyword
 22     public boolean equals(Model obj) {
 23         return this.name.equals(obj.getName()) &&
 24                 Objects.equals(this.salary, obj.getSalary());
 25     }
 26 }
 27 public class Example {
 28 
 29     public static void main(String[] args) {
 30         Model m1 = new Model("Name1", 1000L);
 31         Model m2 = new Model("Name1", 1000L);
 32         Model m3 = new Model("Name3", 3000L);
 33 
 34         boolean b1 = m1.equals(m2);
 35         System.out.println("Does objects m1 and m2 has same values : " + b1);
 36 
 37         boolean b2 = m1.equals(m3);
 38         System.out.println("Does objects m1 and m3 has same values : " + b2);
 39     }
 40 }
In the above example, a class Model define with two properties that initialize using constructor method and compare objects using equals() method that access instance properties using this keyword and compare it with parameterized object and returns boolean value, if properties values are equals.
An object of class Model is created by passing a constructor arguments and calling an instance method by passing an object that compares properties and returns boolean value true, if properties includes same values, otherwise returns false and print.
Output
 1 Does objects m1 and m2 has same values : true
 2 Does objects m1 and m3 has same values : false
Privacy Policy
Terms of Service
Disclaimer
Contact us
About us