Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this considered an inline anonymous method?

Tags:

java

 public void insert(final String key, final String value) throws Exception {
    execute(new Command(){
      public Void execute(final Keyspace ks) throws Exception {
        ks.insert(key, createColumnPath(COLUMN_NAME), bytes(value));
        return null;
      }
    });
  }

The body of new Command() looks like an inline method?

What is this called, I want to fully understand this.

like image 356
Blankman Avatar asked Jul 02 '10 16:07

Blankman


1 Answers

It's an anonymous inner class. You're creating a class which derives from the Command class or just implements the Command interface. In this case you're only overriding one method, but you can override more - as well as having extra fields etc.

Java doesn't (currently) have anything which is quite the equivalent of a C# anonymous method, if that's what you were thinking of. An anonymous inner class is probably the closest thing to it though.

like image 186
Jon Skeet Avatar answered Sep 21 '22 23:09

Jon Skeet