this
Reference to Current Object
Usage:
class Person {
private String name;
private int age;
public void setName(String name) {
this.name = name; // "this" refers to the current Person object
}
}class Rectangle {
private int width;
private int height;
// Default constructor
public Rectangle() {
this(1, 1); // Calling another constructor (constructor chaining)
}
// Parameterized constructor with width and height
public Rectangle(int width, int height) {
this.width = width;
this.height = height;
}
}Scope
No Separate Allocation
Last updated