Use of self-reference

Asked

Viewed 398 times

4

About the use of self-reference thiswanted to know what difference it makes:

public static class Aluno{
    private String nome;

    public String getNome(){
        return nome;
    }
    public Aluno(){
        this.nome="abc";
    }
}  

and to make:

public static class Aluno{
    private String nome;

    public String getNome(){
        return nome;
    }
    public Aluno(){
        nome="abc";
    }
}

what is the difference between this.nome and nome.

4 answers

4

In the shown example there is none, both refer to the field nome.

However, there are situations where this does not happen.
See the case of this example:

public void setNome(String nome){
    this.nome = nome;
}

Here it is necessary to distinguish between the field and the method parameter.

3


The use of this is used in 3 situations:

  1. The most common is used in properties to distinguish method variables from class attributes, ex:

            public class Pessoa{
    
            private String nome;
    
            public void setNome(String nome) {
                //para diferenciar o parametro da variavel utilizamos o this para dizer que é da classe essa variavel.
                this.nome = nome;
            }
    
            // ...
        }
    

The second when there is a need to pass an instance of the current class to another class through parameters.

    public class Carro
    {
    private String nome;

    private Modelo modelo = new Modelo();



    public String Modelo()
    {
        return modelo.CarroModelo(this);
    }

    // ...
    public String getModelo()
    {
        return this.nome;
    }
 }



  public class Modelo
  {

    public String CarroModelo(Carro carro)
    {
        //Instancia do Objeto Carro
        String Nome = carro.getModelo();
        return Nome;
        // ...
    }
}
  1. Calling alternative builders in a class.

     class Pessoa
     {
    
    //Ao instanciar este objeto alem do construtor padrao Pessoa ele irá chamar também o construtor com parametro.
    public Pessoa(){
        this("Construtor com parametro");
    
    
    }
    
    public Pessoa(String nome)
    {
    
    }
    
    
    }
    

1

Although the this in this example you are referring to the same attribute, making no difference at all, in an instance or a constructor this is a reserved word that references the current object - the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor using this.

this: refers to the current instance of the object

As for example in Java we can have a parameter of a method and attribute of a class with the same name. If we make a reference to this variable by the principle of the locality we will be referencing that variable whose declaration is in the case of the parameter. Case we wish to reference the class attribute and not the parameter we should use the reserved word this before the name of variable.

Besides your example, see another example, more visible, in practice as it would be:

Without the this:

public class Point {
    public int x = 0;
    public int y = 0;

    //constructor
    public Point(int a, int b) {
        x = a;
        y = b;
    }
}

With the this:

public class Point {
    public int x = 0;
    public int y = 0;

    //constructor
    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
}

See more details in the documentation.

Other reserved words

See the list of reserved words defined up to version 7 of the language:

package | import | new | class | interface | Enum | Abstract | final | Implements | extends | instanceof | public | private | protected | super | this throw | throws | Try | catch | Finally | if | Else | for | do while switch case | default | break | continue | Return Boolean | byte | short | int | long double | float | char | void | strictfp | Transient volatile | Synchronized | Native | assert | Static goto | const | true | false | null

References

1

In your example there is no difference, are referring to the same attribute.

public static class Aluno{
    private String nome;

    public String getNome(){
        return nome;
    }

    Aluno(){
        this.nome="abc";
    }
}

Now if the example is another, the this will refer to the class attribute.

public static class Aluno{
    private String nome;

    public String setNome(String nome){
        this.nome = nome;
    }
}

In the case of the method setNome(String nome); the this.nome refers to the class attribute Aluno and the nome without the this references the attribute received per parameter in the method.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.