Re: Collections.class methods anachronisms?



jupiter wrote:
The Collections class is supposed to, among other things, return type safe collections from existing collections with static methods such as .checkedList().

My question is: What is so special about these static methods since we can do the same thing by declaring the original list generically with a type like <String>? I mean, once you create the list with <String> the compiler will no longer allow you to add, say, Integer to the list. So what purpose does .checkedList() provide in that context?

Maybe it's all about backward compatibility?



For the most part (where "most part" includes all of the collections interface), all of the type constraints of generics are only checked at compile time. The type-safe methods, e.g., checkedList, are guarantees at /runtime/, something which generics can't do. Observe:

ArrayList<String> foo = new ArrayList();
List bar = foo;
bar.add(5);
String s = foo.get(0).substring(4);

This passes the compiler (although the compiler does give a warning), so the code will be compiled to bytecode where it promptly emits a ClassCastException. Passing the list through to checkedList still gives an error, but it is emitted at the point of modification as opposed to the point of access (which, in some cases, might not even give an error!).

In short, it is mostly a backwards-compatible feature, but it is desirable in circumstances, so it is in no way an anachronism.
.



Relevant Pages

  • Re: Collections.class methods anachronisms?
    ... What is so special about these static methods ... the list with <String> the compiler will no longer allow you to ... all of the type constraints of generics ...
    (comp.lang.java)
  • Collections.class methods anachronisms?
    ... The Collections class is supposed to, among other things, return ... type safe collections from existing collections with static methods ... What is so special about these static methods since ... with a type like <String>? ...
    (comp.lang.java)
  • Re: EnumSet Generics puzzle
    ... >The generic parameterisation is on the method. ... Static methods and static ... Presumably the compiler is deducing the type of E on its own. ... suppose pragmatically you can leave static types out, ...
    (comp.lang.java.programmer)
  • Re: public (static) class method calls private (static) class method, works in records, but not in o
    ... There are several things that the compiler accepts as syntactically valid but which it can't generate code for. ... static methods. ... Dodi reported a problem with inheritance and strings in 2004, and I pointed out that it's not limited to strings -- all compiler-managed types are susceptible to the problem. ...
    (alt.comp.lang.borland-delphi)
  • Re: D8: first impression and uneasy feeling... (long)
    ... At 11:13:06, 18.02.2004, Martin Waldenburg wrote: ... If the compiler sees MyClass.SomeMethod, it will have to look in the ... class for any static methods, and then in the class for any instance ... George Santayana ...
    (borland.public.delphi.non-technical)