In object-oriented programming, the concept of this
in an instance method is a reference to the instance itself that owns the method.
This mechanism allows methods to access and modify the attributes of the instance, as well as invoke other methods defined in the same.
Depending on the programming language, a specific keyword (this
, self
, me
, among others) is used to refer to this internal reference.
However, although they are implemented differently in various programming languages, they basically serve the same purpose.
On the other hand, by its very nature, this
can only be used in instance methods. In static methods, there is no instance, so it doesn’t make sense to use this
.
Practical Case
Let’s look at an example of using this
. In languages that incorporate the concept, it allows referring to the current object within its instance methods.
Suppose the following example with our Person
class. In this case, we have an instance variable called name
. But at the same time, in a method (in this case, in the constructor), we are receiving a variable name
.
class Persona
{
string nombre;
Persona(string nombre)
{
this.nombre = nombre; // 'this' refers to the 'name' field of the class
}
void MostrarNombre()
{
Console.WriteLine("Nombre: " + this.nombre);
}
}
In this case, putting this
allows us to disambiguate between the class members and the method parameters that have the same name.
In this example, this.nombre
refers to the name
field of the Person
class, while name
by itself refers to the constructor parameter.
Could it be better not to use the same name? Well, yes, I won’t deny that 🤷♀️. But it’s just a simple example to illustrate the use of this
.
Return this
In the context of a method, this
is a reference to an object. Something special, but a reference. So it is possible to return this
. In fact, it’s not something rare.
class Persona
{
string Nombre;
Persona()
{
// ... we don't care in this example
}
Persona DoSomething()
{
return this; // can return itself
}
}
Moreover, this enables the creation of certain mechanisms, such as the method chaining pattern or the builder pattern. We will see them in due time.
Examples in Different Languages
Finally, let’s see an example of how self-references work in different programming languages,
In C#, this
is used to refer to the current instance of the class.
public class Persona
{
private string nombre;
public Persona(string nombre)
{
this.nombre = nombre;
}
public void Saludar()
{
Console.WriteLine("Hola, mi nombre es " + this.nombre);
}
}
// Usage
Persona persona = new Persona("Luis");
persona.Saludar(); // Output: Hola, mi nombre es Luis
In C++, this
is a pointer to the current instance of the class.
#include <iostream>
#include <string>
class Persona {
private:
std::string nombre;
public:
Persona(const std::string& nombre) : nombre(nombre) {}
void saludar() const {
std::cout << "Hola, mi nombre es " << this->nombre << std::endl;
}
};
// Usage
int main() {
Persona persona("Luis");
persona.saludar(); // Output: Hola, mi nombre es Luis
return 0;
}
In TypeScript, this
is used within methods to access the attributes and other methods of the instance.
class Persona {
private nombre: string;
constructor(nombre: string) {
this.nombre = nombre;
}
saludar(): void {
console.log("Hola, mi nombre es " + this.nombre);
}
}
// Usage
const persona = new Persona("Luis");
persona.saludar(); // Output: Hola, mi nombre es Luis
In JavaScript, this
is used similarly to TypeScript to refer to the current instance.
class Persona {
constructor(nombre) {
this.nombre = nombre;
}
saludar() {
console.log("Hola, mi nombre es " + this.nombre);
}
}
// Usage
const persona = new Persona("Luis");
persona.saludar(); // Output: Hola, mi nombre es Luis
In Python, self
is used as the first parameter of instance methods to refer to the current instance of the object.
class Persona:
def __init__(self, nombre):
self.nombre = nombre
def saludar(self):
print(f"Hola, mi nombre es {self.nombre}")
# Usage
persona = Persona("Luis")
persona.saludar() # Output: Hola, mi nombre es Luis