• Post Reply Bookmark Topic Watch Topic
  • New Topic

Parameterized Types / Generics  RSS feed

 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Trying to create a static factory method of a parameterized type to be passed to the class of the objects that the factory is creating, but I'm unable to access the type parameters (compile-time error) because the method is static. Is it possible to pass the type parameter to the static method?

The following source attempts to demonstrate what I'm trying to achieve, though it won't compile:



... and a client to the factory would achieve type safety by using something like this (which again, won't compile)



Thanks.
[ November 22, 2004: Message edited by: Tony Morris ]
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
NO!
static members can't use T because T is only defined like:


How could a static member get Type information from an instance?
you can do:

and call the method:
 
Ranch Hand
Posts: 1211
IntelliJ IDE Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mmmm..is this what you are trying to do?

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!