1

I can see in couple of examples on the web a new syntax for me, here's an example:

Accumulator<Integer> sum = new Accumulator<Integer>(){
    public Integer accumulate(Integer t1, Integer t2) {
    return t1+t2;
     }
  };

In general, what does that mean when one writes { a method } after making an instance of a class? Is it some kind of old syntax for something?

Thanks

Numerator
  • 1,389
  • 3
  • 21
  • 40
  • 2
    It's an *anonymous inner class*. See [this answer](http://stackoverflow.com/questions/355167/how-are-anonymous-inner-classes-used-in-java) for more details. – Oliver Charlesworth Sep 07 '11 at 14:34

6 Answers6

5

You're creating an Anonymous inner class.

new Accumulator<Integer>() {
    @Override
    public Integer accumulate(Integer t1, Integer t2) {
        return t1 + t2;
    }
};

Defines an anonymous implementation of Accumulator. You then go on to assign a reference to this anonymous class to sum.

Think of it as a convenient way of simultaneously defining and assigning an implementation. It's a syntactic sugar.

Sahil Muthoo
  • 12,033
  • 2
  • 29
  • 38
  • Does it extends from the class that we have just instanced? – Numerator Sep 07 '11 at 14:38
  • 2-Accumulator is an interface, so it's like we are instancing an interface, but not really- cause we define an Anonymous class, which we don't name cause it's Anonymous, and this new instance will have only the ability that I have just define in this class? Thanks! – Numerator Sep 07 '11 at 14:42
  • 2
    @Sahil: anonymous classes extending concrete types are perfectly legal, and are very often used in Swing programs, for example (anonymous MouseAdapters, KeyAdapters, etc.) – JB Nizet Sep 07 '11 at 14:47
  • @JB [MouseAdapter](http://download.oracle.com/javase/7/docs/api/java/awt/event/MouseAdapter.html) and [KeyAdapter](http://download.oracle.com/javase/7/docs/api/java/awt/event/KeyAdapter.html) are abstract. – Sahil Muthoo Sep 07 '11 at 14:54
  • It's irrelevant. They're abstract because it makes no sense to use them without subclassing them. It would work even if they weren't abstract. Just test it by yourself. – JB Nizet Sep 07 '11 at 14:58
2

No, this is an anonymous inner class. It has the signature of the interface or class of it's type, but can override public methods.

andrewmu
  • 14,276
  • 4
  • 39
  • 37
2

This is called anonymous inner class, i.e. class that does not have name and therefore may be created together with its only one instance. This is the way to decrease number of visible classes in your project when you need to implement some interface and need only one instance of such implementation.

But do not abuse this feature. Use it only if the implementation is trivial (1-2 lines).

AlexR
  • 114,158
  • 16
  • 130
  • 208
1

It's an anonymous inner class which is commonly used.

A quote "An anonymous class is essentially a local class without a name."

1

It is either creating an anonymous subclass with an overloaded method or an anonymous interface implementation (depending on if Accululator is a class or interface).

John B
  • 32,493
  • 6
  • 77
  • 98
1

This is an example of an anonymous class declaration.

Section 15.9.5 of the Java Language Specification discusses these types of declarations.

Brandon E Taylor
  • 24,881
  • 6
  • 47
  • 71