CS310 Notes on Parametrized Types

Starting from Weiss, Sec. 4.7
Parametrized classes
Pg. 131: Note that inside GenericMemoryCell, the type AnyType is considered known. This is like a method with parameter "int x", and we're looking at the method code. There, x is considered known, because at runtime, it has a certain value.  Similarly, inside a generic class, the type is known when the compiler (conceptually) uses this text to create an actual class. In fact, the created class uses Objects instead of a more specific type.

Pg. 133: Figure 4.30 shows the use of a specific type generated from a generic type ArrayList<T>, here ArrayList<Shape>, used a parameter of a method.  As usual, the method can be called using any type that IS-A ArrayList<Shape>. Although Square IS-A Shape (see pg. 111), the rules of generics say that it is not true that ArrayList<Square> IS-A ArrayList<Shape>. That's why the more cumbersome syntax shown in fig. 4.31 was invented, to give us a way to pass types involving subclasses of parameter type classes into a method.

Pg. 133 Generic Static Methods.
Static methods are different from non-static methods of a parametrized class: they can't use the type parameters of the class, which go with the objects of the class, not the class itself. But static methods of parametrized or non-parametrized classes can have their own type parameters. So they can act on parametrized types taken in as method parameters or return types. See figure 4.32 for an example. To call this method, the long form syntax is
    String x = <String>SomeClass.contains(sArray, "foo");  //sArray is of type String[]
but Java will match the type here based on the method arguments, so we can use the shorter form:
    String x = SomeClass.contains(sArray, "foo);

In fact, you can add a type parameter to a non-static method as well. Then it has any type parameters of its class (listed in the class declaration) plus any type parameters of its method (listed in its method declaration). These should all be different, independent type parameters.

Type Erasure: how it works without increasing code size

Restrictions on Generics

instanceof tests
Errata: 3rd line of inline example should be Object list = list1;

Static fields of parametrized classes don't work properly, so just avoid them.

new T() is illegal in class <T>... The class<T> is willing to do anything with a T, but it can't make a T. This is  why the Collection interface has such an odd toArray method: you have to pass in an array to provide the capability to make the array object for return from the method.  One way around this is to have a method in the class API that passes in the class object corresponding to T. The class object can be used to create a new T object with newInstance().

new T[N] is illegal in class <T>, but there is a workaround using casting, as used in figure 4.35, line 38. You need to suppress the warning with @SuppressWarnings("unchecked").

new ArrayList<String>[N] is illegal anywhere. Use new ArrayList<List<String>>(), and then new ArrayList<String>() for the elements.

Skipping to pg. 145: Nested Classes (static inner classes) and generics

A nested class is just a top-level class hidden inside another class. Its objects live independent lives from the outer class. So the type parameters of the outer class don't apply to the nested class.  It can have the same parameters, a parallel construction. If it doesn't have its own parameters, it doesn't have any at all.

Non-static Inner classes 
These don't show up in Weiss until Chap. 15. See pg. 532 for an example of a non-static inner class in a parametrized class ArrayList<AnyType>. You see it can use the parametrized type. Note that it starts out "private class ArrayListIterator ...", with no <AnyType>. That's because as a non-static inner class it belongs to the scope of the <AnyType> of the outer class, and thus doesn't specify (the same) parameters of its own.  If you accidentally put an <AnyType> here, you are shadowing the outer type and causing big problems. In eclipse, you get a confusing warning about AnyType parameter and AnyType type.