Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

generics in static factory methods  RSS feed

 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

what is this???

 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16026
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a static method with type parameters. Not just classes and interfaces can have type parameters; methods can also have type parameters, and the code you posted is an example of that.

See Generic Methods in Oracle's Java Tutorials about generics.
 
Campbell Ritchie
Marshal
Posts: 55678
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to use generics in a static method, you must apply the formal type parameters (e.g. K, V, not k,v) to the method. Since the actual type parameters for the class can vary from instance to instance, they must behave like instance fields, and they are not accessible from a static method.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!