Reputation: 22859
Say I have the following generic class:
public class Foo<T extends Bar> {
// stuff
}
Is it possible to specify a default type(Baz
, for instance), which would act as T
if no T
were passed in?
Upvotes: 4
Views: 2360
Reputation: 12296
Use factory method like
public <T extends Bar> static Foo<T> getInstance(Class<T> clz) {
if (clz == null)
return new Foo<Bar>;
else
return clz.newInstance();
}
Upvotes: 2
Reputation: 86391
No. See "Type Variables" in the Java Language Specification.
That said, you can provide subclasses which accomplish a similar purpose:
public class Foo<T extends Bar> { ... }
public class FooDefault extends Foo< Baz > { ... }
Upvotes: 3