• Post Reply Bookmark Topic Watch Topic
  • New Topic

Generic function  RSS feed

 
Sharon whipple
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to code single function in an interface/class as generic?
For example I must use this :



Without declaring T on Foo :


Thank you
Sharon whipple
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37507
552
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sharon,
Yes. The syntax is:
 
Sharon whipple
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Jeanne !
Its working ok.

If you dont mind, what is the correct signiture for void function?

Thanks again
Sharon
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's the same as Jeanne showed, except there's a void for the return type:

Any time you put <something> just before a method's return type, you are making that method a generic method, and something is the type variable (or variables) for that method.

Note that if you only use a type variable in one place, besides the declaration, then there's really no point in declaring it at all. You might as well use a wild card instead:

It's only when you need a type parameter in two or more places, and you need them to represent the same type, that it becomes useful to declare a type variable such as T - such as you did for getById().
 
Sharon whipple
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excellent explanation!!!
now i got it!

Thank you
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!