Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
A Generic Class is Shared by All Its Invocations
What does the following code fragment print?You might be tempted to sayList <String> l1 = new ArrayList<String>(); List<Integer> l2 = new ArrayList<Integer>(); System.out.println(l1.getClass() == l2.getClass());false
, but you'd be wrong. It printstrue
, because all instances of a generic class have the same run-time class, regardless of their actual type parameters.Indeed, what makes a class generic is the fact that it has the same behavior for all of its possible type parameters; the same class can be viewed as having many different types.
As consequence, the static variables and methods of a class are also shared among all the instances. That is why it is illegal to refer to the type parameters of a type declaration in a static method or initializer, or in the declaration or initializer of a static variable.
Casts and InstanceOf
Another implication of the fact that a generic class is shared among all its instances, is that it usually makes no sense to ask an instance if it is an instance of a particular invocation of a generic type:similarly, a cast such asCollection cs = new ArrayList<String>(); if (cs instanceof Collection<String>) { ...} // Illegal.gives an unchecked warning, since this isn't something the runtime system is going to check for you.Collection<String> cstr = (Collection<String>) cs; // Unchecked warning,The same is true of type variables
Type variables don't exist at run time. This means that they entail no performance overhead in either time nor space, which is nice. Unfortunately, it also means that you can't reliably use them in casts.<T> T badCast(T t, Object o) {return (T) o; // Unchecked warning. }Arrays
The component type of an array object may not be a type variable or a parameterized type, unless it is an (unbounded) wildcard type.You can declare array types whose element type is a type variable or a parameterized type, but not array objects.This is annoying, to be sure. This restriction is necessary to avoid situations like:
If arrays of parameterized type were allowed, the example above would compile without any unchecked warnings, and yet fail at run-time. We've had type-safety as a primary design goal of generics. In particular, the language is designed to guarantee that if your entire application has been compiled without unchecked warnings using javac -source 1.5, it is type safe.List<String>[] lsa = new List<String>[10]; // Not really allowed. Object o = lsa; Object[] oa = (Object[]) o; List<Integer> li = new ArrayList<Integer>(); li.add(new Integer(3)); oa[1] = li; // Unsound, but passes run time store check String s = lsa[1].get(0); // Run-time error: ClassCastException.However, you can still use wildcard arrays. Here are two variations on the code above. The first forgoes the use of both array objects and array types whose element type is parameterized. As a result, we have to cast explicitly to get a
String
out of the array.In the next variation, we refrain from creating an array object whose element type is parameterized, but still use an array type with a parameterized element type. This is legal, but generates an unchecked warning. Indeed, the code is unsafe, and eventually an error occurs.List<?>[] lsa = new List<?>[10]; // OK, array of unbounded wildcard type. Object o = lsa; Object[] oa = (Object[]) o; List<Integer> li = new ArrayList<Integer>(); li.add(new Integer(3)); oa[1] = li; // Correct. String s = (String) lsa[1].get(0); // Run time error, but cast is explicit.Similarly, attempting to create an array object whose element type is a type variable causes a compile-time error:List<String>[] lsa = new List<?>[10]; // Unchecked warning. This is unsafe! Object o = lsa; Object[] oa = (Object[]) o; List<Integer> li = new ArrayList<Integer>(); li.add(new Integer(3)); oa[1] = li; // Correct. String s = lsa[1].get(0); // Run time error, but we were warned.Since type variables don't exist at run time, there is no way to determine what the actual array type would be.<T> T[] makeArray(T t) { return new T[100]; // Error. }The way to work around these kinds of limitations is to use class literals as run time type tokens, as described in the next section, Class Literals as Runtime-Type Tokens.
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.