educative.io

Educative

The usage of "this." inside the methods

Team,

The constructor use its parameters and assigned this.num1 = num1; and this.num2 = num2;

Each method called for this.num1 instead of simply num1. I understand the constructor’s parameters had the same name as the fields, so that’s why we are using this.num1 vs num1.

However, is it best practice to name the constructor parameters something different than the field variables? This would allow us to not have to put this.num1 and this.num2 in every method (repetitive).

Thoughts,

Hi @Steven_Rhyans,

Considering field variables are num1 and num2, if you name the constructor parameters something different even then you’ll have to use this.num1 and this.num2 in every method. Because class’s methods know them by name: num1 and num2. And this keyword is nothing but a reference to the class object.