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:

  • this is an anonymous class definition based on the RowMapper interface, and
  • it creates a single instance of that class?

Thanks in advance.

-------------Problems Reply------------

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.

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!

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).

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.

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.

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

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

new Runnable() {
public void run() {


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


Category:java Views:0 Time:2011-03-03
Tags: java

Related post

  • Free set of forms, icons, styles, etc for web-based admin interfaces 2008-09-28

    Is there any free set of forms, icons, styles, images, etc for building web-based admin interfaces? If yes, which is the best? --------------Solutions------------- A particularly common choice is Silk. It's a very comprehensive free set. There's also

  • Any IoC container that can build types based on interfaces? 2009-09-25

    If I register a bunch of data structures with an IoC container, I'd like to say (C# syntax): var lookup = container.Create<IDictionary<Name,ISequence<EMail>>>() ; The container should magically find the registered types that impleme

  • Text-Based User Interface Development 2009-10-02

    Where can I find resources related to the design and development of text-based user interfaces (e.g. interfaces exported via serial port from embedded devices to VT100 terminals)? I am interested in any material available - best practices, style guid

  • Text Based User Interface 2012-02-09

    I work in a corporate office environment which routinely uses an IBM Mainframe and AS\400 as the main source of the Information System. However, the company is growing faster than the information technology group can support. Therefore, it is necessa

  • Desktop (non-touch) Windows 8.1 with Pen-Based Tablet Interface 2015-02-19

    I recently installed a Wacon Bamboo pen-based touch device onto my Windows 8.1 desktop. My desktop does not have a touch screen. It is a traditional desktop with multiple monitors. The pen-based tablet interface is explicitly meant for Photoshop. How

  • Anonymous types based in Interface 2011-03-02

    This question already has an answer here: Can a C# anonymous class implement an interface? 7 answers Can I create anonymous implementations of an interface , in a way similar to the way delegate() { // type impl here , but not implementing any interf

  • What are the R based web interfaces? What are their features? 2011-11-27

    What are the features of R based Web Consoles? What are their advantages and disadvantages? --------------Solutions------------- That's easy: Rweb is pretty old, and a basic webinterface. The R FAQ lists more such webapps in its Section 4 on R and We

  • Implementing a node-based graphical interface? 2009-03-14

    I would like to implement a nodal-interface, basically a DAG where each node performs an operation on it's input connections, and outputs something (which you can connect to another node) Some example applications: Apples "Shake" - The Foundrys "Nuke

  • Flash, AJAX or something else for a web-based demo interface? 2009-12-03

    My company has some software which we use to acquire data and interact with some hardware. If we wanted to put a mockup on a website which allows the user to get a feel for the UI (gather some data, change some settings etc.), what are some good meth

  • Extension method to filter a list of concrete types based on interface? 2010-02-24

    I have a multiple classes that implement an interface. I want to create an extension method to filter a list of these classes based on a value. Here's a hypothetical example: public enum Transmission { Standard = 1, Automatic = 2 } public interface I

  • Web-Based Configuration Interface for Console Applications 2010-07-07

    I've been using app.Config and INI files to configure my applications but I wanted to make it more user-friendly. I've already encountered some applications ( most of them are java based ) that its configurations are via web interface ( I think the a

  • Code generator to make inline abstract classes that fulfill the role of java anonymous inner classes for C# interfaces? 2010-11-08

    Note: I'm aware that C# has anonymous types and inner types, but these fulfill markedly different roles from the java anonymous inner classes of old. Suppose I have an interface: public interface Foo<T> { int Fizz(string a); T Buzz(object o); v

  • Third-party wanted: generic centralized configuration server with web-based admin interface 2010-11-24

    Rhetorical question--> Why do we always end up developing administration interfaces for web sites, web apps and other types of application? I'm looking for a third-party that would handle the storage, the modeling, the service and the administrati

  • what is the reason for "Anonymous class only implement the interface or extend the class but not both" in java? 2012-02-15

    I have read Anonymous class can only implement the interface or extend the class but not both. But I don't know what is the key reason behind it to prevent both at the same time? Please enlighten me on this... --------------Solutions------------- Bec

  • dnsmasq, serve different ip addresses based on interface used 2012-02-17

    Basically my problem is that I'm running a VM for developing web sites on. The host machine has its dns pointing at the Vm which is running dnsmasq which resolves the addresses of various dev sites, i.e. test.mysite.vm etc. The issue is when I go fro

  • WPF w/ Unity Loading Modules Based on Interface 2013-07-09

    I'm writing a WPF app using Prism/Unity. I have my main app (with a bootstrapper). I have interfaces for each of my modules. For example: MyModuleA implements IMyModuleA MyModuleB implements IMyModuleB I want to implement loose coupling and dependenc

  • GitHub Atom as web-based IDE interface 2014-06-11

    I want to make sure I'm not barking up the wrong tree here. I am currently in a position where I have to use a web based IDE for code changes. As someone who is used to a version controlled, development box type environment it is an inconvenient but

  • Expression generated based on interface 2010-08-31

    I'm geting the exception Unable to cast the type 'MySomeTypeThatImplementsISomeInterfaceAndIsPassedAs[T]ToTheClass' to type 'ISomeInterface'. LINQ to Entities only supports casting Entity Data Model primitive types. my repository looks like public in

  • List Based PHP Interface for multiple actions on multiple elements 2010-11-11

    I've seen this done in a lot of places. I am wondering what the best way to go about implementing this is. Basically what you have is a list of users. The interface shows each user on a line (table?) and has check boxes for options that are displayed

Copyright (C), All Rights Reserved.

processed in 0.141 (s). 11 q(s)