Chapter: Java The Complete Reference - The Java Language - Generics

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail

Some Generic Restrictions - Java

There are a few restrictions that you need to keep in mind when using generics. They involve creating objects of a type parameter, static members, exceptions, and arrays.

Some Generic Restrictions

 

There are a few restrictions that you need to keep in mind when using generics. They 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;

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.

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

 

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.


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail


Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.