• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Anonymous Inner class

 
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am having following code:



In line 4, anonymous class is passed as argument. I wanted to know how is the method foof() of that class getting called.
 
Ranch Hand
Posts: 254
1
MySQL Database Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Foo is not a class; its an interface.

I am not certain but I'll bite. You are creating an anonymous inner class which implements the interface 'Foo' and also provides the implementation of the method foof() (because it has a contract with Foo when it uses `implements`although there is no `implements` and is implied in this case).
 
Rancher
Posts: 1090
14
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I am not certain but I'll bite. You are creating an anonymous inner class which implements the interface 'Foo' and also provides the implementation of the method foof() (because it has a contract with Foo when it uses `implements`).



I'm certain that you're right.

This is one case in which we can type a new <InterfaceName> and the compiler does not complain.

I will just add that you are not only creating an on the spot implementation ( subclass ) of Foo, but also creating an instance of this subclass at the same time and passing this instance as an argument to the method doStuff.

 
Naresh Chaurasia
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually my question arises from the fact because i fail to understand the following code:



From my main, i call getEmployeeById method, which in turn calls jdbcTemplate.queryForObject. In this method the 2nd argument is anonymous class implementing RowMapper, which has method mapRow. How is the method mapRow being called.
 
Ranch Hand
Posts: 530
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Naresh Chaurasia wrote:
From my main, i call getEmployeeById method, which in turn calls jdbcTemplate.queryForObject. In this method the 2nd argument is anonymous class implementing RowMapper, which has method mapRow. How is the method mapRow being called.


Generally anonymous class' methods are called by the calling method's implementation.
In your case, that mapRow() method will be used by the calling method queryForObject() because it known that the passed argument is of type RowMapper which defines the method mapRow().
 
Naresh Chaurasia
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nam Ha Minh wrote:
Generally anonymous class' methods are called by the calling method's implementation.
In your case, that mapRow() method will be used by the calling method queryForObject() because it known that the passed argument is of type RowMapper which defines the method mapRow().




But how is the call made by to mapRow() i.e. how and when it is invoked.
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

But how is the call made by to mapRow() i.e. how and when it is invoked.



I'm not sure, but isn't this part of your question unrelated to the SCJP/OCPJP forum questions? Wouldn't it'd better be in the jdbc and/or frameworks forum.

A google search on 'how RowMapper works with jdbcTemplate' showed me following links where this is sort of explained. A couple of links I found are -

http://www.studytrails.com/frameworks/spring/spring-jdbc-template.jsp
https://coderanch.com/t/447286/Spring/explanation-RowMapper-mapRow-method
https://coderanch.com/t/570063/Spring/RowMapper-callback-interface-mapRow-pull

And is this a crosspost from
http://stackoverflow.com/questions/18534021/how-rowmapper-can-be-an-anonymous-class

Chan.
 
Naresh Chaurasia
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Naresh Chaurasia wrote:
But how is the call made by to mapRow() i.e. how and when it is invoked.



Thanks chan, I got my answer. This is what i had to do.

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic