Java has a special variable called "this" which refers to object we are currently coding in.

The var "this" is always available when coding instance methods.

Example #1
We are coding in class Person, so var "this" refers to a Person object -- the red frame gives the context (boundary) for the "this."

In other words, this applies to what is inside the frame.

Note that all instance methods (e.g., getFirstName, getLastName) have scope (can access) all of Person's ivars (inside the frame).

👉🏼 Try It

📹 Video

public class Person {
	private String firstName;
	private String lastName;

	public Person(String firstName, String lastName) {
		this.firstName = firstName;
		this.lastName = lastName;
	}

	public String getFirstName() {
		return this.firstName;
	}

	public String getLastName() {
		return this.lastName;
	}

	public String toString() {
		return "Person -- " + this.getFirstName()
				+ " " + this.getLastName();
	}

}
Example #2
We are coding in class Rectangle, so var "this" refers to a Rectangle object -- the red frame gives the context (boundary) for "this."

In other words, this applies to what is inside the frame.

Note that all instance methods (e.g., getWidth, getHeight) have scope (can access) all of Rectangle's ivars.

👉🏼 Try It

📹 Video
public class Rectangle {
	private int width;
	private int height;

	public Rectangle(int w, int h) {
		this.width = w;
		this.height = h;
	}

	public int getWidth() {
		return this.width;
	}

	public int getHeight() {
		return this.height;
	}

	public String toString() {
		return "Rectangle -- " + this.getWidth()
				+ " x " + this.getHeight();
	}

}