New to java, help w/ some syntax O_O

Started by
2 comments, last by Fl4sh 13 years, 2 months ago
I'm trying to get into the jmonkey engine when I came across this code in the tuts:


private AnalogListener analogListener = new AnalogListener() {
public void onAnalog(String name, float value, float tpf) {
if (isRunning) {
if (name.equals("Rotate")) {
player.rotate(0, value*speed, 0);
}
if (name.equals("Right")) {
Vector3f v = player.getLocalTranslation();
player.setLocalTranslation(v.x + value*speed, v.y, v.z);
}
if (name.equals("Left")) {
Vector3f v = player.getLocalTranslation();
player.setLocalTranslation(v.x - value*speed, v.y, v.z);
}
} else {
System.out.println("Press P to unpause.");
}
}
};


Why is this syntax allowed in Java? Looks like javascript event listeners. ohmy.gif
They hated on Jeezus, so you think I give a f***?!
Advertisement
looks okay to me. Been using event listeners for a while on android. It uses java for its programming language. Event listeners are used for touch events for example. Also checking for sensor data I think.
[font=arial, verdana, tahoma, sans-serif][size=2]Here analogListener is a member variable of the base-type AnalogListener, that is initialized (i.e. set) at construction time. It is set to an instance of a anonymous class derived from AnalogListener. That sub-type overrides a single method of AnalogListener, namely [color="#000088"]void[color="#000000"] onAnalog[color="#666600"]([color="#660066"]String[color="#666600"], [color="#000088"]float[color="#666600"], [color="#000088"]float[color="#666600"]). The anonymous class will appear like MyClass$1.class (or perhaps an increased number if MyClass has more than one anonymous classes) in the build directory.

The code snippet is equivalent to


public class MyClass {

private class MyListener extends AnalogListener {

public void onAnalog(String name, float value, float tpf) { ... }

}

private AnalogListener analogListener = new MyListener();

}

but avoids the explicit naming of the inner class. As a consequence the anonymous inner class can be instantiated at just a single place in code (because you have no name to refer to it), but that is sometimes sufficient, especially for listeners.

It is further allowed to do this in routines, too. E.g. an alternative would be
[/font]

public class MyClass {

public MyClass() {

this.analogListener = new AnalogListener() {
...
}

}

private AnalogListener analogListener;

}

so it is also possible to choose the concrete anonymous class from code or to instantiate it only if its necessity is determined. This comes in handy especially when a client object is expecting a "listener" instance. E.g.


public class MyClass {

public void doAnything() {

client.addListener(new AnalogListener() {
...
});

}

}

is perfectly valid. Notice the "side-effect" that any non-static inner class has: It inherently refers back to the embedding class, so it can work fine as a mediator between the object listening to and the instance of MyClass, and that without compromising the interface of MyClass with that functionality.
haegarr ftw. ;o
They hated on Jeezus, so you think I give a f***?!

This topic is closed to new replies.

Advertisement