|
| 1 | +--- |
| 2 | +title: Java this keyword |
| 3 | +description: In this tutorial, we will learn the use of 'this' keyword in Java. |
| 4 | +--- |
| 5 | + |
| 6 | +## What is 'this' keyword in Java? |
| 7 | +In Java, the `this` keyword is used within a class to refer to the current instance (object) of that class. It’s commonly used in methods or constructors to reference the current object’s properties or to call other constructors within the same class. |
| 8 | + |
| 9 | +### Example |
| 10 | +```java |
| 11 | +class Main { |
| 12 | + int instVar; |
| 13 | + |
| 14 | + Main(int instVar) { |
| 15 | + this.instVar = instVar; |
| 16 | + System.out.println("this reference = " + this); |
| 17 | + } |
| 18 | + |
| 19 | + public static void main(String[] args) { |
| 20 | + Main obj = new Main(8); |
| 21 | + System.out.println("object reference = " + obj); |
| 22 | + } |
| 23 | +} |
| 24 | +``` |
| 25 | +### Output |
| 26 | +```bash |
| 27 | +this reference = Main@23fc625e |
| 28 | +object reference = Main@23fc625e |
| 29 | +``` |
| 30 | +In this example, `this` refers to the `obj` instance of Main. Both `this` and `obj` share the same memory reference, indicating that this represents the current object. |
| 31 | + |
| 32 | +## Common Uses of the this Keyword |
| 33 | + |
| 34 | +### 1. Resolving Variable Name Conflicts |
| 35 | + |
| 36 | +When instance variables and parameters have the same name, using `this` helps to avoid ambiguity: |
| 37 | + |
| 38 | +```java |
| 39 | +class MyClass { |
| 40 | + int age; |
| 41 | + |
| 42 | + MyClass(int age) { |
| 43 | + this.age = age; // Uses `this` to differentiate instance variable from parameter |
| 44 | + } |
| 45 | +} |
| 46 | +``` |
| 47 | +Without `this`: |
| 48 | + |
| 49 | +```java |
| 50 | +class Main { |
| 51 | + int age; |
| 52 | + Main(int age) { |
| 53 | + age = age; // No `this`, so no assignment to the instance variable |
| 54 | + } |
| 55 | + |
| 56 | + public static void main(String[] args) { |
| 57 | + Main obj = new Main(8); |
| 58 | + System.out.println("obj.age = " + obj.age); // Output: obj.age = 0 |
| 59 | + } |
| 60 | +} |
| 61 | +``` |
| 62 | +### 2. Using this with Getters and Setters |
| 63 | + |
| 64 | +`this` is often used in setter methods to differentiate between instance variables and parameters: |
| 65 | + |
| 66 | +```java |
| 67 | +class Main { |
| 68 | + String name; |
| 69 | + |
| 70 | + void setName(String name) { |
| 71 | + this.name = name; |
| 72 | + } |
| 73 | + |
| 74 | + String getName() { |
| 75 | + return this.name; |
| 76 | + } |
| 77 | + |
| 78 | + public static void main(String[] args) { |
| 79 | + Main obj = new Main(); |
| 80 | + obj.setName("Toshiba"); |
| 81 | + System.out.println("obj.name: " + obj.getName()); |
| 82 | + } |
| 83 | +} |
| 84 | +``` |
| 85 | +### Output |
| 86 | +```bash |
| 87 | +obj.name: Toshiba |
| 88 | +``` |
| 89 | + |
| 90 | +### 3. Constructor Overloading with this() |
| 91 | +In constructor overloading, `this()` can be used to call another constructor within the same class, reducing code duplication: |
| 92 | + |
| 93 | +```java |
| 94 | +class Complex { |
| 95 | + private int a, b; |
| 96 | + |
| 97 | + // Constructor with two parameters |
| 98 | + Complex(int i, int j) { |
| 99 | + this.a = i; |
| 100 | + this.b = j; |
| 101 | + } |
| 102 | + |
| 103 | + // Single parameter constructor calls two-parameter constructor |
| 104 | + Complex(int i) { |
| 105 | + this(i, i); |
| 106 | + } |
| 107 | + |
| 108 | + // No-argument constructor calls single-parameter constructor |
| 109 | + Complex() { |
| 110 | + this(0); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public String toString() { |
| 115 | + return this.a + " + " + this.b + "i"; |
| 116 | + } |
| 117 | + |
| 118 | + public static void main(String[] args) { |
| 119 | + Complex c1 = new Complex(2, 3); |
| 120 | + Complex c2 = new Complex(3); |
| 121 | + Complex c3 = new Complex(); |
| 122 | + |
| 123 | + System.out.println(c1); // Output: 2 + 3i |
| 124 | + System.out.println(c2); // Output: 3 + 3i |
| 125 | + System.out.println(c3); // Output: 0 + 0i |
| 126 | + } |
| 127 | +} |
| 128 | +``` |
| 129 | +Here, `this()` helps to manage multiple constructors, improving readability and reducing code duplication. However, it should be used carefully, as excessive use can slow down the program. |
| 130 | + |
| 131 | +### 4. Passing this as an Argument |
| 132 | +You can use `this` to pass the current object as an argument to other methods: |
| 133 | + |
| 134 | +```java |
| 135 | +class ThisExample { |
| 136 | + int x; |
| 137 | + int y; |
| 138 | + |
| 139 | + ThisExample(int x, int y) { |
| 140 | + this.x = x; |
| 141 | + this.y = y; |
| 142 | + |
| 143 | + System.out.println("Before passing this:"); |
| 144 | + System.out.println("x = " + this.x + ", y = " + this.y); |
| 145 | + |
| 146 | + add(this); |
| 147 | + |
| 148 | + System.out.println("After passing this:"); |
| 149 | + System.out.println("x = " + this.x + ", y = " + this.y); |
| 150 | + } |
| 151 | + |
| 152 | + void add(ThisExample obj) { |
| 153 | + obj.x += 2; |
| 154 | + obj.y += 2; |
| 155 | + } |
| 156 | + |
| 157 | + public static void main(String[] args) { |
| 158 | + ThisExample obj = new ThisExample(1, -2); |
| 159 | + } |
| 160 | +} |
| 161 | +``` |
| 162 | +### Output |
| 163 | + |
| 164 | +```bash |
| 165 | +Before passing this: |
| 166 | +x = 1, y = -2 |
| 167 | +After passing this: |
| 168 | +x = 3, y = 0 |
| 169 | +``` |
| 170 | +In this example, `this` allows the `add()` method to modify the current instance variables directly. |
| 171 | + |
| 172 | +## Key Points |
| 173 | +- **Variable Disambiguation:** `this` resolves conflicts when instance variables and parameters share the same name. |
| 174 | +- **Getters and Setters:** `this` is often used in setters to distinguish instance variables. |
| 175 | +- **Constructor Overloading:** `this()` can call other constructors within the same class, helping to reduce code redundancy. |
| 176 | +- **Passing Current Object:** `this` allows you to pass the current object as a method argument, enabling modifications on the current instance. |
0 commit comments