2

Can anything other than null be added to this s1 ? ( and be safely assigned to temp )

Is

<S extends ArrayList<S>>

a useful construct in the language or just a grammar side-effect? Am I correct in interpreting S as an ArrayList that can only hold objects that are ArrayLists of ArrayLists of ArrayLists, etc?

public class Foo{
    public static <S extends ArrayList<S>> void f1() {
        S s1 = (S) new ArrayList<S>();

        // s1.add( ???? );  // can anything be added here other than null?
        S temp = s1.get(0);
    }
}
Gonen I
  • 5,576
  • 1
  • 29
  • 60
  • 1
    [XY Problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) perhaps? – Hovercraft Full Of Eels Feb 02 '17 at 22:15
  • You told Java that S is a subclass of ArrayList here: `>` – Jason Feb 02 '17 at 22:16
  • Am not asking something I know the answer to , if that's what you mean. Null is all I found that works so far. And I know I told Java what S is. I mean who technically supplies the parameter, since main is not called explicitly. – Gonen I Feb 02 '17 at 22:18
  • But what are you trying to do? Do you ask out of academic interest, or is there a problem you are trying to solve? – Hulk Feb 02 '17 at 22:20
  • It was discovered as part of a separate problem, and now just curious. Now it is not for a problem I am trying to solve. The cyclic reference was real, and is the most interesting part here. I don't understand completely how java treats it. – Gonen I Feb 02 '17 at 22:22

1 Answers1

3

You can only add s1 and null to it without casting s1 or casting other objects to S:

s1.add(s1);
s1.add(s1.get(0));

You know no other objects in the scope that fulfill the S extends ArrayList<S> constraint as type parameter S is only declared in this scope. That is the reason why it is considered bad practice to define a generic type parameter for a method in the case that you don't use it at more than one place in the method signature.

Calculator
  • 2,769
  • 1
  • 13
  • 18
  • Interesting. Is this S extends ArrayList construct useful in any way or just a grammar side affect? – Gonen I Feb 02 '17 at 22:27
  • 1
    @user889742 There are use cases - not with Collections, usually - more things like the ones discussed in this question - [What does > mean in Java?](http://stackoverflow.com/q/18892061/2513200) – Hulk Feb 02 '17 at 22:30
  • Good point about the casting too. I will specify it in the question. Obviously you can add anything to it by putting it in a raw ArrayList ref. – Gonen I Feb 02 '17 at 22:32
  • 1
    @user889742 self recursive type parameters are often used in parameterized classes. Famous example: [Enum](http://stackoverflow.com/q/211143) – Calculator Feb 02 '17 at 22:33
  • and I can also add other new objects of type ArrayList to s1, not just add s1 to itself. – Gonen I Feb 02 '17 at 22:43
  • @user889742 No you can only add objects of type `S`. You would have to cast a new `ArrayList` to `S` to be able to add it to s1. – Calculator Feb 02 '17 at 22:49
  • True, but in this case the cast is correct, and would hold up during a run time retrieval, so I would consider it valid. Notice that It is in fact how s1 itself is created. A cast is absolutely necessary there, but correct. – Gonen I Feb 02 '17 at 22:55