What is the use of .this in Java [duplicate]

1

Hello, so I'm starting in java and I'd like to get a better understanding of what .this is. I guess it has something to do with the current class or something of the genre. But I'm not sure. I'll post a part of my code that .this is contained

   class Conta {  
   int numero;
   String dono;
   double saldo;
   double limite; 
  // Os metodos que ultilizam o .this
   boolean saca(double valor) {
         if (this.saldo < valor) {
           return false;
         }
         else {
           this.saldo = this.saldo - valor;
           return true;
         }
   }
      void deposita(double quantidade) {
         this.saldo += quantidade;
   }
}

It has the public class also that is only giving value to the variables created in the class Account

    
asked by anonymous 19.03.2016 / 06:56

1 answer

1

This is to differentiate when the method has the same parameter name as the class, in your code this does not happen, so you do not need this, if you had any attribute in the class with the value name, then that method I would need this.value, to say that you want to change only the attribute of that instance and not the class

I'm not sure if this is the case.

But roughly so, if your method passes parameter with the same name as an attribute of the class, then to refer to that instance of the method, if this is used, otherwise you will understand that change the class attribute

    
19.03.2016 / 07:34