• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question about generics...  RSS feed

 
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have the following method:



I am having trouble understanding the <T> after the visibility of the method (public) and before the return type (void)? What does it mean?

public<T> void

Thanks in advance,

Julien.
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The void is just a regular void return type. The method you specified cannot return a value.

<T> is a parameterising the method, making it specific to whatever type you want to pass in. It's more for clarity than functional in that example. You could make it more useful by doing something like the following to limit the list that can be passed in to something like List<Integer> etc:



Hope this helps
 
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With other words, it's the *declaration* of T. Without it, the identifier T would be unknown in the rest of the method.
 
Julien Martin
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot to both of you for your replies.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!