0

Java – 5 things You Cannot do with Generic Types

Generics is one of the best things that happened to Java. Anyone who has worked with Java during its pre-JDK5 era can attest to that. For new developments, it has drastically reduced the amount of written codes and provided compile-time type checking using Generic Types. However, there are some things you cannot do with Generics Types.

Call their Constructors

Generic Types cannot be instantiated using its constructor. For example:

The compile-time error is as follows:

Create Arrays

We cannot create an array of a Generic Type.

The compile-time error is as follows:

Call instanceof

Calling instanceof on Generic Types results in compile-time errors.

The compile-time error is as follows:

Use primitives as Generic Type Parameters

Generics only deal with classes and objects.

The compile-time error is as follows:

Create static variable

Generic Types cannot be used to create static variables.

The compile-time error is as follows:

 

Karl San Gabriel

Karl San Gabriel

Java and Enterprise Technologies Expert