Annotate the toString() method with @Override or not and use this or not in the context of a Java class, is there anything definite about this?

Question:

It's a question I've had for a long time and I can't find anything concrete about it.

In any class, if I want to override the toString() method, and use the properties of the class.

It works for me if I annotate the method with @Override and if I use this :

@Override
public String toString () 
{
    return String.format("Nombre: %s, Apellido: %s",this.firstName,this.lastName);
}

And if I don't annotate and/or use this , it also works:

public String toString () 
{
    return String.format("Nombre: %s, Apellido: %s",firstName,lastName);
}

There are two (x2) questions in one:

  • Should it be annotated with @Override or not, why or why not? Why does it work both ways?

  • Should this be used or not, why or why not? Why does it work both ways?

I've been researching this but can't find anything specific. I want to know if there is any answer, documentation or established practice that defines what should be done in these cases, to give uniformity to my code since sometimes I use it or stop using it, since it works in both ways.

Answer:

@Override

Should it be annotated with @Override or not, why or why not? Why does it work both ways?

Indeed, it works both ways. The @Override annotation helps developers to know at a glance that there is a method of a parent class that has been overridden by a child class . In this case, the .toString() method of the String class.

Este

Should this be used or not, why or why not? Why does it work both ways?

this is used to specifically indicate that the instance variable is used instead of the static or local variable . That is to say:

public class Persona {

    private final String nombre;

    public Persona(String nombre) {
        // como podríamos inicializar el valor nombre usando el parámetro?
        // no podemos hacer: nombre = nombre;
    }
}

In the example above, we want to assign the field member using the value of the parameter. Since they share the same name, we need a way to distinguish between the field and the parameter. this allows us to access the members of this instance, including the field .

public class Persona {
    private final String nombre;

    public Persona(String nombre) {
        this.nombre= nombre;
    }
}

Therefore we could not say that in this particular case, it works anyway. In case of not using this , we are forced to give a different name to the two variables.

Looking at the example you gave, @Override could be considered a good practice, and with this you are specifying that you mean the instance variable. In case you have a method like this in the future:

public String toString (String firstName, String lastName) 
{
    return String.format("Nombre: %s, Apellido: %s",this.firstName,this.lastName);
}

You are telling the compiler that you mean the instance variable and not the ones you are receiving as parameters: (String firstName, String lastName)

Scroll to Top