Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Method-Local Inner Classes?

 
Minhaj Mehmood
Ranch Hand
Posts: 400
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone give me the example of Method-Local Inner Classes on-job(usage in real life) example?
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have never used one and never found the reason to use one.

I did encounter it once when maintaining some code. A programmer had used a local class to hold the values that a JDBC database call returned. These values were put into an object and used immediately. It was a pretty long method. I thought it made the method look ugly and if the class were to be used somewhere else, it would not be possible. I try to avoid method local inner classes
 
Waclaw Borowiec
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found these kind of classes useful in a scenario when I was sending a message to another part of a system and I waited for an asynchronous reply in a separate thread. The code looked somewhat like this:



ReplyThread is not used anywhere else bedside this method so in terms of scope there's no need for it to be declared on higher level i.e. as an inner or a top level class. Of course for someone readability can have higher priority than the narrowest possible scope.
 
Minhaj Mehmood
Ranch Hand
Posts: 400
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see - Thanks for the code Waclaw Borowiec
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic