Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile error when using multiple generic bounds

Tags:

java

generics

I am trying to define a generic method whose parameter is bound by two types, but it doesn't seem to work:

public static <T extends Readable, Appendable> void doSomething(T t) {
    int r = t.read(...); // compiles OK
    //
    t.append(...); // compile error
}

This should compile. Can anyone see what I'm doing wrong?

like image 782
Bohemian Avatar asked Apr 25 '26 21:04

Bohemian


1 Answers

The problem is that the method defines two parameters, one called T and one called Appendable, not one parameter with two bounds.

The comma should be an ampersand, like this:

public static <T extends Readable & Appendable> void doSomething(T t) {
    //
}
like image 99
Bohemian Avatar answered Apr 27 '26 12:04

Bohemian



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!