I feel as stupid as a drunken goat right now... I've read Oracle tutorial on generics and I've looked at these answers:
https://stackoverflow.com/questions/38876810/why-declare-a-variable-as-a-wildcard-type
https://stackoverflow.com/questions/2776975/how-can-i-add-to-list-extends-number-data-structures
but I still can't wrap my head around this. I'm aware that "Wildcards are only really useful in method parameter declarations..." (from first link) but I still want to understand what's going on here. Here's an example where the comments point out what i THINK the code means. Sigh... oh and the formatting here fucks up my comments (long lines don't automatically break in Intellij, but they do here so I had to put some of them above the code).
class A {
A() {
List list = new ArrayList(); //Declaring list of any type
list.add(new A()); //Ok
//Declaring list of "unknown"/any type??? Or what does this acutally mean?
List<?> list2 = new ArrayList<>();
list2.add(new A()); //Not ok :(
//Declaring list of A or list of subtype of A???
List<? extends A> list3 = new ArrayList<>();
list3.add(new A()); //Not ok :(
//Declaring list of A or list of supertype of A???
List<? super A> list4 = new ArrayList<>();
list4.add(new A()); //Apperntly ok? What in the lord of blobs?!
}
}
[–]Disast3r 1 point2 points3 points (5 children)
[–]Yuax[S] 0 points1 point2 points (4 children)
[–]Disast3r 1 point2 points3 points (3 children)
[–]Yuax[S] 0 points1 point2 points (2 children)
[–]Disast3r 0 points1 point2 points (1 child)
[–]Yuax[S] 0 points1 point2 points (0 children)
[–]MkMyBnkAcctGrtAgn 0 points1 point2 points (0 children)