I met such a code

public EWrapperImpl() { readerSignal = new EJavaSignal(); clientSocket = new EClientSocket(this, readerSignal); } 

When instantiating a clientSocket pass it to the this constructor.

What is it? Instance copy? What EWrapperImpl ... EWrapperImpl ? Or EClientSocket ?

    2 answers 2

    The keyword this always a reference to the instance of the class in which it is used. In your case, this is EWrapperImpl

    It should be noted that this rule also applies to nested classes. For example:

     public class Outher { public Outher() { //в этом месте this - это ссылка на класс Outher } class Inner { public Inner() { //в этом месте this - это ссылка на класс Inner //а чтобы во внутреннем классе получить ссылку на внешний класс //надо делать так ИмяВнешнегоКласса.this, т.е так: //Outher.this } } } 

      this is a reference to the current object — an object whose method or constructor is being called. As I understand it, your complete construction looks like this :

       public class EWrapperImpl implements EWrapper { private EReaderSignal readerSignal; private EClientSocket clientSocket; protected int currentOrderId = -1; public EWrapperImpl() { readerSignal = new EJavaSignal(); clientSocket = new EClientSocket(this, readerSignal); } } 

      Here this is a link to an instance of the EWrapperImpl class.

      • Thank. Apparently you know this API once wrote readerSignal = new EJavaSignal (); - RVG
      • You are welcome. No, I'm not familiar with this. - post_zeew