Reputation: 2975
I have started JAVA and doing serial communication using RxTx.
Referring to: http://rxtx.qbang.org/wiki/index.php/Event_based_two_way_Communication http://henrypoon.wordpress.com/2011/01/01/serial-communication-in-java-with-example-program/
In the 2nd link I am not able to decipher usage of 'this': Can anyone please explain:
Communicator.java
public class Communicator implements SerialPortEventListener
{
GUI window = null;
..
..
public Communicator(GUI window)
{
this.window = window;
}
...
..
}
In GUI.java
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);
}
...
..}
I am confused how this is used to create an object of Communicator class, as highlighted in above code(appearing communicator = new Communicator(this);)
Another confusion is: Communicator.java
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");
}
}
....
}
Again I am confused with the use of 'this' here (serialPort.addEventListener(this);)
I compared with the code at http://rxtx.qbang.org/wiki/index.php/Event_based_two_way_Communication
there it suggests
...
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()) > -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);
}
}
}
The description for addEventListener http://docs.oracle.com/cd/E17802_01/products/products/javacomm/reference/api/
addEventListener
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.
I want to know usage of this as how it is passing 'SerialPortEventListener lsnr' as the parameter to addEventListener in above code.
Thanks
Upvotes: 0
Views: 198
Reputation: 213243
this
keyword is a reference to the current instance for which the code is being executed. So, since this
is a reference, you can use it as any other reference. No problem in that.
Now let's take a look at your usage: -
new Communicator(this);
Since this statement is used inside the method of GUI
class, so, this
refers to the instance of GUI
, currently executing the code. Now, by passing it to the constructor, you are simply passing the reference of current instance to it. And it's quite valid, since Communicator
constructor takes a reference of type GUI
: -
public Communicator(GUI window)
{
this.window = window;
}
Now let's move ahead with the next statement:
serialPort.addEventListener(this);
Here, you are registering the serialPort
with an EventListener
which is referenced by this
. Since, this is used inside the class - Communicator
, which implements SerialPortEventListener
, so basically you are registering to a Communicator
instance, which is nothing but a SerialPortEventListener
. So, you are registering to that event.
As far as your other code is concerned:
serialPort.addEventListener(new SerialReader(in));
Here, you have just used a new instance
instead of this
, since you are not inside SerialReader
class. So, you don't have this
reference to any SerialReader
instance, and hence you need to create an object manually of that class.
So, there is no difference. Because, in any case, you are registering the class that implements SerialPortEventListener
only.
Upvotes: 1
Reputation: 45083
There's a lot to digest here to answer your original question, and I'm not sure it all relates, but let's tackle that specifically. The following code demonstrates use of this
to remove ambiguity in the current scope:
public Communicator(GUI window)
{
this.window = window;
}
this
represents the current instance of the current class. So in your first instance of confusion, this
is an instance of Communicator
and so this.window
exists. In the second case this
is an instance of GUI
, hence can be passed as a parameter to Communicator
, as that's what it accepts.
Within that constructor for Communicator
, because two things exist with the same name, and are to be used in the same scope, we need to disambiguate what we're doing. And we do that by saying "assign the foreign window
thing to our local, known, owned window
thing."
Think of a group of people, with numerous John's, and you simply shouting that name. Who would look to respond? What if you pointed, or there was some other indicator, such as the right John recognises you when they all look? this
is that indicator, giving the specificity required to make something distinct from another.
Otherwise imagine the confusion of trying to assign window
to window
. Do we mean assign the local version to the local version, the parameter to the parameter, the parameter to the local version, or the local version to the parameter?
Upvotes: 0
Reputation: 15683
this
is a pseudo-variable meaning 'the object that this method was called on' (or in a constructor: 'the object that is currently being constructed'). I have no idea what your problem with the code you posted is, because it includes lots of code that is apparently irrelevant to the question and I'm not going to start guessing where exactly your problem with them lies.
Upvotes: 0