I have a class containing a static create method.
public class TestClass {
public static TestClass create() {
return new TestClass&l
Assuming you are asking about specifying the type explicitly in case type inference fails, you can use TestClass.<String>create()
(notice how the type is after the .
as opposed to before).
The generic type can be specified in the class declaration:
public class TestClass<E> {
public static <E> TestClass<E> create() {
return new TestClass<E>();
}
}
// Elsewhere in the code
TestClass<String> testClass = TestClass.create();
Yeah, that's pretty... unintuitive.
A sidenote from Josh Bloch's presentation of his Effective Java, 2nd Edition about the issue: "God kills a kitten every time you specify an explicit type parameter". I would like to avoid constructs like this but sometimes it cannot be evaded.
The trick is to specify the generic parameter after the .
character: TestClass.<String>create()
.