bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/Java/Java Classes
Java•Java Classes

Java this

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind Java this?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

public ___ Main {
3Order

Put the learning moves in the order that makes the concept easiest to apply.

The this keyword in Java refers to the current object in a method or constructor.
Calling a Constructor from Another Constructor
Accessing Class Attributes

Java this Keyword

The this keyword in Java refers to the current object in a method or constructor.

The this keyword is often used to avoid confusion when class attributes have the same name as method or constructor parameters.

Accessing Class Attributes

Sometimes a constructor or method has a parameter with the same name as a class variable. When this happens, the parameter temporarily hides the class variable inside that method or constructor.

To refer to the class variable and not the parameter, you can use the this keyword:

Example

public class Main {
  int x;  // Class variable x
  // Constructor with one parameter x public Main(int x) { this.x = x; // refers to the class variable x
}
public static void main(String[] args) {
  // Create an object of Main and pass the value 5 to the constructor Main myObj = new Main(5); System.out.println("Value of x = " + myObj.x);
}
}

Tip

Think of this.x = x; as: " this.x (the class variable) gets the value of x (the parameter)."

Without this , the code above x = x; would set the parameter x equal to itself, and the class variable would stay uninitialized ( 0 ).

Calling a Constructor from Another Constructor

You can also use this() to call another constructor in the same class.

This is useful when you want to provide default values or reuse initialization code instead of repeating it.

Example

public class Main {
  int modelYear;
  String modelName;
  // Constructor with one parameter public Main(String modelName) { // Call the two-parameter constructor to reuse code and set a default year this(2020, modelName);
}
// Constructor with two parameters public Main(int modelYear, String modelName) { // Use 'this' to assign values to the class variables this.modelYear = modelYear; this.modelName = modelName;
}
// Method to print car information public void printInfo() { System.out.println(modelYear + " " + modelName);
}
public static void main(String[] args) {
  // Create a car with only model name (uses default year) Main car1 = new Main("Corvette"); // Create a car with both model year and name Main car2 = new Main(1969, "Mustang"); car1.printInfo(); car2.printInfo();
}
}

Note

The call to this() must be the first statement inside the constructor.

When to use this?

  • When a constructor or method has a parameter with the same name as a class variable, use this to update the class variable correctly.
  • To call another constructor in the same class and reuse code.

Previous

Java Constructors

Next

Java Modifiers