'this' keyword: Using it as parameter
<p>I have started JAVA and doing serial communication using RxTx.</p>

<p>Referring to: 
<a href="http://rxtx.qbang.org/wiki/index.php/Event_based_two_way_Communication" rel="nofollow">http://rxtx.qbang.org/wiki/index.php/Event_based_two_way_Communication</a>
<a href="http://henrypoon.wordpress.com/2011/01/01/serial-communication-in-java-with-example-program/" rel="nofollow">http://henrypoon.wordpress.com/2011/01/01/serial-communication-in-java-with-example-program/</a></p>

<p>In the 2nd link I am not able to decipher usage of <strong>'this'</strong>:
Can anyone please explain:</p>

<p><strong>Communicator.java</strong> </p>

<pre><code>public class Communicator implements SerialPortEventListener
{
  GUI window = null;
   ..
   ..
 public Communicator(GUI window)
    {
        this.window = window;
    }

...
..
}
</code></pre>

<p><strong>In GUI.java</strong></p>

<pre><code>public class GUI extends javax.swing.JFrame {
Communicator communicator = null;
Communicator communicator = null;
    //KeybindingController object
    KeybindingController keybindingController = null;

    /** Creates new form GUI */
    public GUI() {
        initComponents();
        createObjects();
        communicator.searchForPorts();
        keybindingController.toggleControls();
        keybindingController.bindKeys();
    }

    private void createObjects()
    {
        **communicator = new Communicator(this);**
        keybindingController = new KeybindingController(this);
    }
...
..}
</code></pre>

<p>I am confused how this is used to create an object of Communicator class, as highlighted in above code(appearing <strong>communicator = new Communicator(this);</strong>)</p>

<p>Another confusion is:
<strong>Communicator.java</strong> </p>

<pre><code>public class Communicator implements SerialPortEventListener
{ 
...
...
 public void connect()
    {
        String selectedPort = (String)window.cboxPorts.getSelectedItem();
        selectedPortIdentifier = (CommPortIdentifier)portMap.get(selectedPort);

        CommPort commPort = null;

        try
        {
            //the method below returns an object of type CommPort
            commPort = selectedPortIdentifier.open("TigerControlPanel", TIMEOUT);
            //the CommPort object can be casted to a SerialPort object
            serialPort = (SerialPort)commPort;
....
...}


 public void initListener()
    {
        try
        {
            **serialPort.addEventListener(this);**
            serialPort.notifyOnDataAvailable(true);
        }
        catch (TooManyListenersException e)
        {
            logText = "Too many listeners. (" + e.toString() + ")";
            window.txtLog.setForeground(Color.red);
            window.txtLog.append(logText + "\n");
        }
    }
....
}
</code></pre>

<p>Again I am confused with the use of 'this' here  (<strong>serialPort.addEventListener(this);</strong>)</p>

<p>I compared with the code at 
<a href="http://rxtx.qbang.org/wiki/index.php/Event_based_two_way_Communication" rel="nofollow">http://rxtx.qbang.org/wiki/index.php/Event_based_two_way_Communication</a></p>

<p>there it suggests</p>

<pre><code>...
InputStream in = serialPort.getInputStream();
**serialPort.addEventListener(new SerialReader(in));**
...

 public static class SerialReader implements SerialPortEventListener 
    {
        private InputStream in;
        private byte[] buffer = new byte[1024];

        public SerialReader ( InputStream in )
        {
            this.in = in;
        }

        public void serialEvent(**SerialPortEvent arg0**) {
            int data;

            try
            {
                int len = 0;
                while ( ( data = in.read()) &gt; -1 )
                {
                    if ( data == '\n' ) {
                        break;
                    }
                    buffer[len++] = (byte) data;
                }
                System.out.print(new String(buffer,0,len));
            }
            catch ( IOException e )
            {
                e.printStackTrace();
                System.exit(-1);
            }             
        }

    }
</code></pre>

<p>The description for addEventListener
<a href="http://docs.oracle.com/cd/E17802_01/products/products/javacomm/reference/api/" rel="nofollow">http://docs.oracle.com/cd/E17802_01/products/products/javacomm/reference/api/</a></p>

<p>addEventListener</p>

<p>public abstract void addEventListener(SerialPortEventListener lsnr)
                               throws java.util.TooManyListenersException
Registers a SerialPortEventListener object to listen for SerialEvents. Interest in specific events may be expressed using the notifyOnXXX calls. The serialEvent method of SerialPortEventListener will be called with a SerialEvent object describing the event.</p>

<p>I want to know usage of this as how it is passing 'SerialPortEventListener lsnr' as the parameter to addEventListener in above code.</p>

<p>Thanks</p>
