What will be the result of compilation and execution of the following code?

import java.util.*;

class AClass { }

public class Test {    
    public static void main (String... args) { 
      ArrayList<AClass> a = new ArrayList<AClass>(); 
      AClass aaaClass = new AClass();
      a.add(aaaClass);
      staticMethod1(a); 
      staticMethod2(a);
      System.out.println(a.size()); 
    } 
    
    static <T super ArrayList<AClass>> void staticMethod1(T a) {   // 1
     AClass c = new AClass();
     a.add(c);
    }

    static <T extends ArrayList<AClass>> void staticMethod2(T a) {  // 2
     AClass c = new AClass();
     a.add(c);
    }    
}
Explanation
Keyword super in generics is used for setting a bounded wildcard.
The correct use of generics would be:

static <T extends ArrayList<? super AClass>> void staticMethod1(T a) {   // 1
    AClass c = new AClass();
    a.add(c);
}
In that case it would print out: 2

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
Java quizzes
Cosmo
Sign Up Now
or Subscribe for future quizzes