1 Reply Latest reply: Jul 2, 2010 1:42 AM by 843793 RSS

    Any way to assign value for  variable of type Class<List<String>> ?

    843793
      I'm puzzled why I can declare a variable:
      Class<List<String>> clazz;
      but I cannot assign a value to it as
       clazz = List<String>.class;
      The compiler also complains about
       Class<List<?>> clazz0 = List<?>.class;
      yet it has the nerve to warn about raw types when I give up and use
       Class<List> clazz0 = List.class;
      Even using a dummy instance does not work.
              List<String> dummy = new ArrayList<String>();
              Class<List<String>> clazz1 = dummy.getClass();
      I only care because I've declared a method like
      public String useless( Class<List<String>> unSetable){
        return  unSetable.getName();
      }
      And now there seems to be no way to call it.
        • 1. Re: Any way to assign value for  variable of type Class<List<String>> ?
          843793
          Hello chymes,

          there is no way to get an instance of Class<List<String>> without at least one unchecked warning. Otherwise you could get away with the following:
          List<Integer> ints = new ArrayList<Integer>();
          Class<List<String>> clazz = List<String>.class;
          List<String> strings = clazz.cast(ints);
          strings.add("No Good");
          int i = ints.get(0); // CCE
          Therefore the only way to get it is via unchecked cast:
          Class<List<String>> clazz = (Class<List<String>>) (Object) List.class;
          With kind regards
          Ben