Hi guys is there anyway that I can reference or use this anonymous inner class in other classes?
private DgtebdllLib.CallbackFunctionCharPtr getScan = new DgtebdllLib.CallbackFunctionCharPtr()
The full code is below and I found this Java code here. This is an example for interfacing with DGT chessboard DLL using Java. What I just like to do is just to get the data str String variable from getScan anonymous inner class. Any help/suggestions is deeply appreciated. Thanks
public class DemoDll
{
private static boolean running = true;
private static DgtebdllLib dll = null;
/**
* Callback implementation function for the received status
*/
private DgtebdllLib.CallbackFunctionCharPtr getStatus = new DgtebdllLib.CallbackFunctionCharPtr()
{
@Override
public void callback(Pointer data)
{
String str = data.getString(0);
System.out.println("Received status: " + str);
}
};
/**
* Callback implementation function for the evaluation of the received scan
*/
private DgtebdllLib.CallbackFunctionCharPtr getScan = new DgtebdllLib.CallbackFunctionCharPtr()
{
@Override
public void callback(Pointer data)
{
String str = data.getString(0);
System.out.println("Received a scan: " + str);
}
};
It seems you are asking the wrong question. The call back encapsulates an action and all you have to do is to implement the desired action. So If you want to store the received data, just store it, e.g. in the outer instance:
public class DemoDll {
private static boolean running = true;
private static DgtebdllLib dll = null;
String status;
String scan;
/**
* Callback implementation function for the received status
*/
private DgtebdllLib.CallbackFunctionCharPtr getStatus
= new DgtebdllLib.CallbackFunctionCharPtr() {
@Override
public void callback(Pointer data) {
status = data.getString(0);
System.out.println("Received status: " + status);
}
};
/**
* Callback implementation function for the evaluation of the received scan
*/
private DgtebdllLib.CallbackFunctionCharPtr getScan
= new DgtebdllLib.CallbackFunctionCharPtr() {
@Override
public void callback(Pointer data) {
scan = data.getString(0);
System.out.println("Received a scan: " + scan);
}
};
…
Now you can access the most recent status
and scan
values received by the call-backs without any special “access anonymous inner classes” feature.
Alternatively you can make the values accessible through a member class which is not anonymous:
public class DemoDll {
private static boolean running = true;
private static DgtebdllLib dll = null;
static class StringRef implements DgtebdllLib.CallbackFunctionCharPtr() {
String value;
@Override
public void callback(Pointer data) {
value = data.getString(0);
System.out.println("Received: " + value);
}
}
StringRef status;
StringRef scan;
…
so then you can use status
and scan
as call-backs and read status.value
and scan.value
afterwards.