0

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?

francium
  • 2,384
  • 3
  • 20
  • 29

2 Answers2

2

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.

Community
  • 1
  • 1
TheNorthWes
  • 2,661
  • 19
  • 35
1

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

Eray Balkanli
  • 7,752
  • 11
  • 48
  • 82