0% found this document useful (0 votes)
16 views3 pages

Generic Restrictions

There are some restrictions when using generics in Java. Type parameters cannot be instantiated, static members cannot use type parameters, and arrays cannot be instantiated with a type parameter. Generic classes also cannot extend Throwable to create generic exceptions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views3 pages

Generic Restrictions

There are some restrictions when using generics in Java. Type parameters cannot be instantiated, static members cannot use type parameters, and arrays cannot be instantiated with a type parameter. Generic classes also cannot extend Throwable to create generic exceptions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

CompRef_2010 / Java The Complete Reference, Ninth Edition /Schildt / 007180 855-8

Chapter 14 Generics 377

Some Generic Restrictions


There are a few restrictions that you need to keep in mind when using generics. They

Part I
involve creating objects of a type parameter, static members, exceptions, and arrays. Each
is examined here.

Type Parameters Can’t Be Instantiated


It is not possible to create an instance of a type parameter. For example, consider this class:

// Can't create an instance of T.


class Gen<T> {
T ob;

Gen() {
ob = new T(); // Illegal!!!
}
}

Here, it is illegal to attempt to create an instance of T. The reason should be easy to


understand: the compiler does not know what type of object to create. T is simply a
placeholder.

Restrictions on Static Members


No static member can use a type parameter declared by the enclosing class. For example,
both of the static members of this class are illegal:

class Wrong<T> {
// Wrong, no static variables of type T.
static T ob;

// Wrong, no static method can use T.


static T getob() {
return ob;
}
}

Although you can’t declare static members that use a type parameter declared by the
enclosing class, you can declare static generic methods, which define their own type
parameters, as was done earlier in this chapter.

Generic Array Restrictions


There are two important generics restrictions that apply to arrays. First, you cannot
instantiate an array whose element type is a type parameter. Second, you cannot create
an array of type-specific generic references. The following short program shows both
situations:

// Generics and arrays.


class Gen<T extends Number> {
T ob;

14-ch14.indd 377 14/02/14 5:05 PM


CompRef_2010 / Java The Complete Reference, Ninth Edition /Schildt / 007180 855-8

378 PART I The Java Language

T vals[]; // OK

Gen(T o, T[] nums) {


ob = o;

// This statement is illegal.


// vals = new T[10]; // can't create an array of T

// But, this statement is OK.


vals = nums; // OK to assign reference to existent array
}
}

class GenArrays {
public static void main(String args[]) {
Integer n[] = { 1, 2, 3, 4, 5 };

Gen<Integer> iOb = new Gen<Integer>(50, n);

// Can't create an array of type-specific generic references.


// Gen<Integer> gens[] = new Gen<Integer>[10]; // Wrong!

// This is OK.
Gen<?> gens[] = new Gen<?>[10]; // OK
}
}

As the program shows, it’s valid to declare a reference to an array of type T, as this line
does:
T vals[]; // OK

But, you cannot instantiate an array of T, as this commented-out line attempts:


// vals = new T[10]; // can't create an array of T

The reason you can’t create an array of T is that there is no way for the compiler to know
what type of array to actually create.
However, you can pass a reference to a type-compatible array to Gen( ) when an object
is created and assign that reference to vals, as the program does in this line:
vals = nums; // OK to assign reference to existent array

This works because the array passed to Gen has a known type, which will be the same type
as T at the time of object creation.
Inside main( ), notice that you can’t declare an array of references to a specific generic
type. That is, this line
// Gen<Integer> gens[] = new Gen<Integer>[10]; // Wrong!

won’t compile.

14-ch14.indd 378 14/02/14 5:05 PM


CompRef_2010 / Java The Complete Reference, Ninth Edition /Schildt / 007180 855-8

Chapter 14 Generics 379

You can create an array of references to a generic type if you use a wildcard, however, as
shown here:

Part I
Gen<?> gens[] = new Gen<?>[10]; // OK

This approach is better than using an array of raw types, because at least some type
checking will still be enforced.

Generic Exception Restriction


A generic class cannot extend Throwable. This means that you cannot create generic
exception classes.

14-ch14.indd 379 14/02/14 5:05 PM

You might also like