Word reserved this as parameter

1

I have the following code:

public abstract class Notific<T extends Notifi> {

    public final void send() {
        NotificationEventPublisherFactory.createInstance().publishEvent(this);
    }

}

What is this? the method? the class?

    
asked by deluf 21.05.2017 в 17:47
source

4 answers

2

Although from the perspective of the Java programmer does not allow the use of pointers, the reserved word this is a pointer whose memory address points to an object of the type of the class where the word has been written.

    
answered by 24.05.2017 в 06:23
1

The reserved word 'this' was created in principle to avoid conflicts between global and local variables. Of course, the question that will come to mind is why do not I give it a different name?

At the time of developing longer and longer programs involved a lot of variables and find different names will lose time to the programmer and obviously make more expensive the routine task of following the code.

"the reference this always points to the object on which a method is executed."

Note that this does not point to variables, but to objects, that is, if you do not have attributes in your class and you will use this , nothing would happen, compile the same. When you do this , you access the variables of the object and not the local variables of its methods.

That's why it's important to know that, if you invoke this from a static method (you do not need to instantiate a class to be used) this it will not work because it will not have any object to point to.

So in your case, this will point to an object in the class NotificationEventPublisherFactory .

    
answered by 21.05.2017 в 18:49
1

this is not a method or class, this is a reserved word in java. What's the use, I'll briefly comment:

This is used to indicate the reference to the current object, it is generally used when there is ambiguity between attributes and parameters methods, that is, when it has the same name, for example:

      public class Empleado {
             private int valor
        public Empleado(int valor){
              this.valor=valor
          } 
      }

Another use that we can give you is to invoke constructors of the same class, the invocation must always be on the same line, for example:

    public class Empleado {
        private int valor;
    public Empleado(){
        this(125); //Este llama al segundo constructor
        } 
     }  
     public Empleado(int valor){
        this.valor=valor
      } 

Now when we use this as an argument in a method, we are sending an object of the class, for example:

public class Simuladores {

    private int atributo1;
    private int atributo2;

    public static void main(String[] args) {
        // TODO code application logic here
    Simuladores obj= new Simuladores();
    obj.callThis();

    }
private void callThis(){
    setAtributo1(10);
    setAtributo2(5);
    testThis(this); // se envia un objeto del tipo Simuladores

}
    private void testThis(Simuladores aThis) {
     // aThis contiene los atributos de la clase

       int valor1 = aThis.atributo1;
       int valor2 = aThis.atributo2;
       System.out.println(valor1);
       System.out.println(valor2);

    }
  

Correction in definition.

    
answered by 21.05.2017 в 18:28
0

The word this is a self-reference. The word this allows in blocks of code to refer to the current object. In a method it refers to the object to which the method is called. An example:

Notific n = new Notific();
// lamada a send() de n
n.send();
// en el caso de tu objeto equivalente a:                       v aquí la referencia
NotificationEventPublisherFactory.createInstance().publishEvent(n);

You can also use this to call other constructors within a constructor, or to disambiguate between a field and a local variable:

'public clase MiClase{

     int valor=0;

     public MiClase(int valor){
         // asignar la variable local al campo del mismo nombre:
         this.valor=valor;
     }

     public MiClase(){
         // el constructor sin valor asume un valor inicial de 100 por defecto:
         this(100); 
     }
 }

In this case'new MyClass ()% co_of% new MyClass (100) '.

    
answered by 21.05.2017 в 20:33