Jump to content

  • Log In with Google      Sign In   
  • Create Account

New to java, help w/ some syntax O_O


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 Fl4sh   Banned   -  Reputation: 30

Like
0Likes
Like

Posted 24 February 2011 - 11:02 PM

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. Posted Image
They hated on Jeezus, so you think I give a f***?!

Sponsor:

#2 Enders   Members   -  Reputation: 128

Like
0Likes
Like

Posted 24 February 2011 - 11:47 PM

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.

#3 haegarr   Crossbones+   -  Reputation: 4603

Like
1Likes
Like

Posted 25 February 2011 - 02:05 AM

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 void onAnalog(String, float, float). 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

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.

#4 Fl4sh   Banned   -  Reputation: 30

Like
0Likes
Like

Posted 25 February 2011 - 03:56 PM

haegarr ftw. ;o
They hated on Jeezus, so you think I give a f***?!




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS