• Advertisement
Sign in to follow this  

New to java, help w/ some syntax O_O

This topic is 2521 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to get into the jmonkey engine when I came across this code in the tuts:
[code]

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.");
}
}
};
[/code]

Why is this syntax allowed in Java? Looks like javascript event listeners. [img]http://public.gamedev.net/public/style_emoticons/default/ohmy.gif[/img]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[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 [i]anonymous[/i] class derived from AnalogListener. That sub-type overrides a single method of AnalogListener, namely [color="#000088"]void[/color][color="#000000"] onAnalog[/color][color="#666600"]([/color][color="#660066"]String[/color][color="#666600"],[/color] [color="#000088"]float[/color][color="#666600"],[/color] [color="#000088"]float[/color][color="#666600"])[/color]. 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

[code]
public class MyClass {

private class MyListener extends AnalogListener {

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

}

private AnalogListener analogListener = new MyListener();

}[/code]
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
[/size][/font]
[code]
public class MyClass {

public MyClass() {

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

}

private AnalogListener analogListener;

}[/code]
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.

[code]
public class MyClass {

public void doAnything() {

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

}

}[/code]
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement