It's because it's using generics in a confusing way.
Where it says
class Test<String>, this means "create a class with the generic type
String, where
String can stand for any class". Then, within the class,
String is interpreted as "whatever the generic type is", instead of interpreting it as a shorthand for
java.lang.String.
This is why we usually use
T or something else that doesn't clash with a real class name. The class you've written is identical to this one:
And now it should be much more clear what's happening. Lines 3 and 4 are fine. On line 5 it's assigning a
java.lang.String to an unknown type. It won't allow this without an explicit cast. This is what we're doing on line 2, which compiles but may throw a
ClassCastException at runtime.
And on line 6 it's trying to instantiate a
T directly, which isn't allowed (for a start, you don't know what constructors
T will have).
Edit: snap (almost)