Reputation: 2480
I was looking at the java 7 api, specifically DualPivotQuickSort.java, and noticed they're not using generics and instead overloading for each type. Is there a specific reason for this?
Upvotes: 0
Views: 74
Reputation: 7990
Static fields of type parameters are not allowed to be used with generics since static type fields are shared by non-static fields in classes. See the following example:
public class Vehicle<T> {
private static T item;
// ...
}
If static fields of type parameters were allowed like above, then it would be confusing to decide the type of item for the definitions below:
Vehicle<Car> car= new Vehicle<>();
Vehicle<Ship> ship= new Vehicle<>();
Vehicle<Train> train= new Vehicle<>();
Vehicle<Bus> bus= new Vehicle<>();
Since the static field is shared with car,ship,train and bus, and it cannot be all at the same time, it is not possible to decide the actual type of the item.
Reference: https://docs.oracle.com/javase/tutorial/java/generics/restrictions.html#createStatic
Upvotes: 1
Reputation: 2739
Everything in that class is Static
and in Java, generics are not in scope for a static method. See here for more information, and it is generally well discussed around the web.
I guess as to answer why, and just to hazard a guess, to fit the pattern of how other sorts are implemented.
Upvotes: 2