Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Java generics in an anonymous inner class [duplicate]

I have edited my example to show a more practical example of what I'm trying to achieve. I would like to understand what I should specify as the second type when instantiating the new TableCell object within the Lambda expression, given the types being passed into the method.

Thanks in advance for any help.

void setTableCellStuff (TableColumn <Object, ?> tableCell) {
    tableCell.setCellFactory(arg0 -> 
        new TableCell <Object, ?> () // cannot use ? in instantiator.
    );
}
like image 282
Matt Avatar asked May 20 '26 02:05

Matt


1 Answers

You probably want this:

<T> void setTableCellStuff (TableColumn <Object, T> tableCell) {
    tableCell.setCellFactory(arg0 -> 
        new TableCell <Object, T> ()
    );
}

T could still be any type, but this way the type of table cell you construct matches the type of column you have.

like image 99
Matt Timmermans Avatar answered May 21 '26 15:05

Matt Timmermans



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!