Mike M. Lin
Mike M. Lin

Reputation: 10072

Anonymous class definition based on interface... maybe?

I saw this Java snippet in the book Spring in Action, but I'm not familiar with the language construct.

new RowMapper() {
  public Object mapRow() throws SQLException, DataAccessException {
    Motorist motorist = new Motorist();
    motorist.setId(rs.getInt(1));
    motorist.setEmail(rs.getString(2));
    motorist.setPassword(rs.getString(3));
    motorist.setFirstName(rs.getString(4));
    motorist.setLastName(rs.getString(5));
    return motorist;
  }
}

According the Spring documentation, RowMapper is an interface. It looks to me like an anonymous class definition based on the RowMapper interface. The new keyword is a little confusing, making me wonder if this also creates one instance of the anonymous class. I would guess yes, because if the class has no name, how will you ever create an instance after the line that defines it?

Can anyone confirm my guesses that:

Upvotes: 15

Views: 26586

Answers (6)

abishkar bhattarai
abishkar bhattarai

Reputation: 7641

Declaring Anonymous class and in below example it creates two instances .

public class Multithread {
    void test(){
    new Runnable() {
        @Override
        public void run() {

            System.out.println("1");
        }
     }.run();
     new Runnable() {
         @Override
         public void run() {

             System.out.println("11");
         }
      }.run();}



    public static void main(String[] args) {
        new Multithread().test();
      }



}

Upvotes: 0

Margus
Margus

Reputation: 20038

Interface tells what methods the built class instance should have or if thy are label interfaces, then what kind of behavior to associate with it.

Anonymous classes are classes that basically while instantiating a class instance thy are also extending it with custom code. So if you are instantiating a interface, then you must write all the methods described with that interface, and as long as you do at least that much, then compiler will be happy. This is what is done here.

IS this is an anonymous class definition based on the RowMapper interface?

Yes. As you can see mapRow() function has been written. And if you debug the code you can see, that is not a class of an instance of interface, but class that extends interface. In case of abstract class or just class, it would be same - extended. So if class is final you cant write anonymous class for it.

Does it create a single instance of that class?

Well, it extends it and makes an instance of it. It will be single instance and any sequent call to it would result in a different class. If you debug the code, then you can even see different class names dynamically associated with it.

Upvotes: 2

Aasmund Eldhuset
Aasmund Eldhuset

Reputation: 37950

Your guesses are entirely correct. An anonymous class definition may be based on either a non-final class or on an interface, and you must implement all abstract (or interface) methods. The only available syntax for declaring anonymous classes is new, which also has the effect of instantiating exactly one instance of the anonymous class (in the course of the program, though, many instances of the same anonymous class could be created, if this code is executed several times).

Upvotes: 3

adarshr
adarshr

Reputation: 62583

That code is implementing the interface in an anonymous way.

The syntax would be similar to:

Runnable runnable = new Runnable() {
    public void run() {
    }
};

Note the semicolon at the end of the declaration. Here the runnable object, though holds the reference to the Runnable interface actually contains the implemented object. That's runtime polymorphism for you!

Upvotes: 6

Martin Klinke
Martin Klinke

Reputation: 7332

Solely from the code above and without knowing about RowMapper, all you can assume is that a new anonymous class based on RowMapper (which may be an interface or a class) is instantiated.

Upvotes: 0

aioobe
aioobe

Reputation: 420951

This is an anonymous class definition based on the RowMapper interface

That's precisely what it is.

It creates a single instance of that class?

Yep. That's correct.

Upvotes: 13

Related Questions