# Accessing functions from Java Generics

**URL:** <https://discourse.processing.org/t/accessing-functions-from-java-generics/3391>\
**Category:** Coding Questions\
**Created:** [September 9, 2018, 12:29pm UTC](https://discourse.processing.org/t/accessing-functions-from-java-generics/3391 "2018-09-09T12:29:26Z")\
**Posts on this page:** 1\
**Showing post:** 9

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [September 10, 2018, 10:16am UTC](https://discourse.processing.org/t/accessing-functions-from-java-generics/3391/9 "2018-09-10T10:16:01Z")

</div>

> [@Sarah](#):
>
> If I declare `<T>` as a PVector , I want to be able to access the PVector’s _x_ and _y_.

Even If we had 2 or more classes w/ the exactly same code, Java would still “see” them as different from each:

```auto
void setup() {
  MyVec vec1 = new MyVec();
  MyVecTwin vec2 = new MyVecTwin();

  println(MyVec.class.isInstance(vec1)); // true
  println(MyVec.class.isInstance(vec2)); // false

  exit();
}

class MyVec {
  float x = random(-100, 100), y = random(-100, 100), z;

  @Override String toString() {
    return "[" + x + ", " + y + ", " + z + "]";
  }
}

class MyVecTwin { // exactly same code as class MyVec
  float x = random(-100, 100), y = random(-100, 100), z;

  @Override String toString() {
    return "[" + x + ", " + y + ", " + z + "]";
  }
}

```

So it doesn’t matter if a class got structurally the same members as another 1; under Java’s PoV, they’re not the same! 🤪

In my previous reply, I’ve instantiated a Comparator which can be used to **sort()** any array or list of objects which `implements` the DoubleSupplier datatype.

If we needed to **sort()** a container of PVector objects instead, we’d need to change the Comparator’s generic datatype DoubleSupplier to PVector.

Plus, switch method DoubleSupplier::**getAsDouble()** to field PVector::_x_ inside method Comparator::**compare()**'s implementation:

```auto
import java.util.Comparator;
import java.util.Arrays;
//import java.util.Collections;

static final Comparator<PVector> ASCENT_X = new Comparator<PVector>() {
  @Override final int compare(final PVector v1, final PVector v2) {
    return (int) Math.signum(v1.x - v2.x);
  }
};

```

Still, we couldn’t **sort()** neither an array of MyVec nor of MyVecTwin w/ the Comparator\<PVector\> above; even though they’ve both got the field _x_ in common, just like a PVector: 😞

```auto
import java.util.Comparator;
import java.util.Arrays;
//import java.util.Collections;

static final Comparator<PVector> ASCENT_X = new Comparator<PVector>() {
  @Override final int compare(final PVector v1, final PVector v2) {
    return (int) Math.signum(v1.x - v2.x);
  }
};

final PVector[] vecs = {
  PVector.random2D(this).mult(100), 
  PVector.random2D(this).mult(100), 
  PVector.random2D(this).mult(100)
};

final MyVec[] myVecs = { new MyVec(), new MyVec(), new MyVec() };

class MyVec {
  float x = random(-100, 100), y = random(-100, 100), z;

  @Override String toString() {
    return "[" + x + ", " + y + ", " + z + "]";
  }
}

void setup() {
  println(vecs);
  Arrays.sort(vecs, ASCENT_X);
  println(vecs);
  println();

  println(myVecs);
  //Arrays.sort(myVecs, ASCENT_X); // Error: MyVec isn't a PVector!!!
  //println(myVecs);

  exit();
}

```

---

_[View the full topic](https://discourse.processing.org/t/accessing-functions-from-java-generics/3391)._
