Results 1 to 2 of 2
  1. #1
    coolman50544 is offline Member
    Join Date
    Apr 2014
    Location
    Miami, FL
    Posts
    47
    Rep Power
    0

    Default I don't understand this syntax

    Hi guys. I'm learning how to use this game engine, and came across this code.

    Java Code:
       
    private ActionListener actionListener = new ActionListener() {
            public void onAction(String name, boolean isPressed, float tpf) {
                System.out.println("Your triggered " + name);
            }
        };
    The thing is that I just don't understand the syntax. I see that an object is being created, but then there is a { right after. Inside the curly braces is a method named onAction(). Then the final }; has a ; at the end. I don't really understand what is going on here, and how this is possible. Could someone explain to me what this means? Thanks

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,616
    Rep Power
    5

    Default Re: I don't understand this syntax

    It's an anonymous class and it is overriding the onAction method. Anonymous classes are used quite a bit
    to implement simple listeners. IMHO they are fine for single method interfaces, but I prefer private inner classes which
    extend adapters for the multi-method interfaces. And with Java 8 and the new Lambda syntax, the above syntax is
    somewhat passe.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Dot Syntax
    By jdodle in forum New To Java
    Replies: 4
    Last Post: 11-09-2013, 08:20 PM
  2. Don't understand this Java syntax.
    By Kareng in forum New To Java
    Replies: 9
    Last Post: 09-25-2012, 04:05 PM
  3. trying to understand generic Syntax
    By cris9288 in forum New To Java
    Replies: 1
    Last Post: 02-17-2012, 06:47 PM
  4. Syntax question
    By rockeater in forum New To Java
    Replies: 3
    Last Post: 08-31-2010, 11:52 PM
  5. Java syntax I don't understand - assignment
    By poet in forum New To Java
    Replies: 2
    Last Post: 09-19-2009, 06:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •