Java, Java Tutorial, Software Development

Java – 5 things You Cannot do with Generic Types – Limitations

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 thing you cannot do with Java Generics types, i.e., limitations.

Generics Limitation 1 – We Cannot Call their Constructors

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

 

The compile-time error is as follows:

Generics Limitation 2 – We cannot Create Arrays

We cannot create an array of a Generic Type.

The compile-time error is as follows:

Generics Limitation 3 – We Cannot use Call instanceof with Generics Types

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

The compile-time error is as follows:

Limitation 4 – We Cannot Use primitives as Generic Type Parameters

Generics only deal with classes and objects.

The compile-time error is as follows:

Limitation 5 – We Cannot Create static variables with Generics

Generic Types cannot be used to create static variables.

The compile-time error is as follows:

 

Loading

Got comments or suggestions? We disabled the comments on this site to fight off spammers, but you can still contact us via our Facebook page!.


You Might Also Like