Introduction
The this keyword helps you refer to the current object inside a class. It makes your code clear and avoids confusion when names overlap.
Jump into concepts and practice - no test required
The this keyword helps you refer to the current object inside a class. It makes your code clear and avoids confusion when names overlap.
this.fieldName this.methodName() this(parameters) // to call another constructor
this always refers to the current object instance.
Use this to avoid confusion between local variables and object fields.
this.color means the field of the object, and color is the constructor parameter.class Car { String color; Car(String color) { this.color = color; // 'this.color' is the field, 'color' is the parameter } }
this.name refers to the field, so we can set it using the parameter name.class Person { String name; void setName(String name) { this.name = name; // Assign parameter to the object's field } }
this(10, 20) calls another constructor in the same class.class Box { int width, height; Box() { this(10, 20); // Calls the other constructor } Box(int w, int h) { width = w; height = h; } }
This program shows how this assigns the constructor parameter to the object's field and then prints it.
public class ThisExample { int x; ThisExample(int x) { this.x = x; // Assign parameter x to field x } void printX() { System.out.println("Value of x: " + this.x); } public static void main(String[] args) { ThisExample obj = new ThisExample(5); obj.printX(); } }
You cannot use this in static methods because static methods belong to the class, not an object.
Using this improves code readability and helps avoid mistakes when names overlap.
this refers to the current object instance.
Use this to access fields or methods of the current object.
this can call another constructor in the same class.
this keyword refer to in a Java class?thisthis keyword always points to the current object instance inside a class.this = current object instance [OK]this means "this object" [OK]this with static contextthis refers to superclassthis is a local variablethis to call another constructor in the same class?this(); as the first statement.super(); calls superclass constructor, this.call(); is invalid syntax, and call(this); is not a constructor call.this(); [OK]this(); to call another constructor [OK]super(); instead of this();this(); not as first statementclass Test {
int x = 10;
void printX() {
int x = 20;
System.out.println(x);
System.out.println(this.x);
}
public static void main(String[] args) {
new Test().printX();
}
}printX(), local x is 20, instance x is 10.this.x usageSystem.out.println(x); prints local 20, System.out.println(this.x); prints instance 10.this for instance [OK]this keywordclass Sample {
int value;
Sample(int value) {
value = value;
}
}value = value; assigns the parameter to itself, not to the instance variable.thisthis.value = value;.this. causes no instance update [OK]this.variable = variable; to assign correctly [OK]this keywordclass Point {
int x, y;
Point() {
this(0, 0);
}
Point(int x, int y) {
this.x = x;
this.y = y;
}
void move(int x, int y) {
x = x;
this.y = y;
}
String display() {
return "(" + this.x + ", " + this.y + ")";
}
public static void main(String[] args) {
Point p = new Point();
p.move(5, 10);
System.out.println(p.display());
}
}main method?this(0, 0); setting x=0 and y=0.move methodmove, x = x; assigns parameter to itself, so instance x remains 0. this.y = y; updates instance y to 10.display output(0, 10).this.x unchanged, this.y updated [OK]this.var = var; inside methods [OK]x = x; updates instance variable