I to know if it is possible to have an if statement in a method that will check the type of the generic used. In the case that got me thinking about this I want to handle a pretty basic Point2D
class and a Point3D
class differently. In the case of the 3D point I need to access Point3d.z
, and I am not sure whether or not this will cause problems A pseudo code version of what I would like to do is
public <T> void processPoints(T point) {
process(point.x);
process(point.y);
if (T == Point3D) { // What do I do here?
process(point.z); // Will accessing z cause problems?
}
}
In reality the code process
represents is a lot more complicated and z
is dependent on x
and y
so I am looking for a way to avoid code duplication. I will probably figure out a way to overload the function instead, but I am curious so I can learn more about generics.