I create a generic method without parameter, some thing like:
private <T> TableCell<T> createTableCell(){
return new TableCell<T>();
}
So, in my program, how to call this method for a concrete type?
I create a generic method without parameter, some thing like:
private <T> TableCell<T> createTableCell(){
return new TableCell<T>();
}
So, in my program, how to call this method for a concrete type?
Usually, the type is inferred, but you can specify the type with this syntax:
Note: You have an error in your method's definition - it had no return type:
private <T> TableCell<T> createTableCell(){
return new TableCell<T>();
}
Here's how you can call it:
TableCell<SomeType> tableCell = myObject.<SomeType>createTableCell();
If you method doesn't access any fields, consider making it a static
method, which you would call like:
TableCell<SomeType> tableCell = MyClass.<SomeType>createTableCell();
As an aside, when you use this syntax, many will marvel at your "eliteness" - it's a syntax not often seen.
Because the type can not be inferred from the context (when you call the method) you have to specify it when calling in the folowing way:
obj.<MyType>createTableCell()
where obj
is the object of a class/type that contains that method.
You'd call this method the same way you called the constructor inside it: createTableCell<TypeName>()
.