No static member can use a type parameter, but is it possible to call a static member using the generic type parameter? For example:-
abstract class Agent<A>{
void callAgent();
Agent(){
A.add();
}
}
Here add() is a static method.
There are some C# questions and answers on a similar topic but I’m not too sure how to go about it in Java.
No you cannot do it if A is a generic type. (Bozho answered to fast 🙂 and probably thought A was concrete type.
What will work is the following.
but it’s probably not what you want to do.
After reading your comments it sounds like what you really want to do is:
Your subclasses will have to override
createNew().If you still do not like that you can take a look at AspectJ which will allow you to do some constructor magic (see how spring does @Configurable) but that gets far trickier and complicates things.
Another option is Scala. Java does not do inheritance on static methods so you can’t get parameterized modules (groups of functions in some languages this is called a functor … ocaml). However Scala supports a singleton “object” that does allow for parametric functional polymorphic inheritance.