I struggle with the following inner class "inheriting" the generic parameter T
from the outer class. The following lines are erroneous:
String string = "string";
new OuterClass<>()
.inner(string)
.call(s -> s.length()); // Cannot resolve method 'length' in 'Object'
Minimal and reproducible example of these classes:
public class OuterClass<T> {
public InnerClass<T> inner(T object) {
return new InnerClass<>(object);
}
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public static final class InnerClass<U> {
U object;
public final void call(Consumer<U> consumer) {
consumer.accept(object);
}
}
}
Why does this happen? How am I supposed to specify the generic parameter through the creation of the inner class?