• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Anonymous Inner class

 
Naresh Chaurasia
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
  • 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.
 
Ahsan Bagwan
Ranch Hand
Posts: 254
1
Java MySQL Database Ubuntu
  • Mark post as helpful
  • send pies
  • 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).
 
Chan Ag
Rancher
Posts: 1089
14
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Nam Ha Minh
Ranch Hand
Posts: 508
Eclipse IDE Firefox Browser Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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: 1089
14
  • Mark post as helpful
  • send pies
  • 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
http://www.coderanch.com/t/447286/Spring/explanation-RowMapper-mapRow-method
http://www.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
  • 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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic