Using auto reference

4

On the use of auto reference this wanted to know what difference to make:

public static class Aluno{
    private String nome;

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

and what to do:

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 .

    
asked by anonymous 18.01.2017 / 12:33

4 answers

2

The use of this is used in 3 situations:

  • The most common is used in properties to distinguish method variables with class attributes, eg

            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;
            }
    
            // ...
        }
    
  • 2. The second when there is a need to pass an instance of the current class to another class via 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;
            // ...
        }
    }
    
  • Calling alternative constructors 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)
    {
    
    }
    
    
    }
    
  • 18.01.2017 / 12:58
    3

    In the example shown there are none, both refer to the nome attribute.

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

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

    Here you need to distinguish between the attribute and the method parameter.

        
    18.01.2017 / 12:53
    1

    In your example there is no difference, you 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, 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 method setNome(String nome); , this.nome refers to the attribute of class Aluno and nome without this references the attribute received by parameter in the method.

        
    18.01.2017 / 12:59
    1
    Although this in this example is referring to the same attribute, making no difference, 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 that.

      

    this : refers to the current instance of the object

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

    In addition to your example, see another example, more visible in practice as it would be:

    Without this :

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

    With 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 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 | end |   implements | extends | instanceof | public | private | protected |   super | this throw | throws | try | catch | finally | if | else | for   | 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

    18.01.2017 / 12:42