# Questions on static methods

**URL:** <https://discourse.processing.org/t/questions-on-static-methods/6169>\
**Category:** Coding Questions\
**Created:** [December 2, 2018, 5:51pm UTC](https://discourse.processing.org/t/questions-on-static-methods/6169 "2018-12-02T17:51:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [December 2, 2018, 5:51pm UTC](https://discourse.processing.org/t/questions-on-static-methods/6169/1 "2018-12-02T17:51:25Z")

</div>

Hi everyone,

I’m trying to get my head around static methods in a class.

Let’s say I want to create the following class:

```auto
class MyVector {
  float v1, v2;
  
  MyVector(float v1, float v2) {
    this.v1 = v1;
    this.v2 = v2;
  }
}

```

Now let’s imagine that I want a way to create a unit vector to the right, something like this:

```auto
MyVector createUnitVector() {
  return new MyVector(1, 0);
}

```

My first guess was to create a static method inside my `MyVector` class like so:

```auto
class MyVector {
  float v1, v2;

  MyVector(float v1, float v2) {
    this.v1 = v1;
    this.v2 = v2;
  }

  public static MyVector createUnitVector() {
    return new MyVector(1, 0);
  }
}

```

But in order to do so, I need to also make my class static:

```auto
static class MyVector {
  ...
}

```

For now it’s okay, but let’s imagine that I want to add a randomize function:

```auto
static class MyVector {

  ...

  public void randomize() {
    v1 = random(1);
    v2 = random(1);
  }
}

```

Now I can’t do that since the `random()` function is a non-static method of the PApplet class.

Of course I could simply put my function outside the class like so:

```auto
class MyVector {
  float v1, v2;

  MyVector(int v1, int v2) {
    this.v1 = v1;
    this.v2 = v2;
  }

  public void randomize() {
    v1 = random(1);
    v2 = random(1);
  }
}

MyVector createUnitVector() {
  return new MyVector(1, 0);
}

```

But is there a more “elegant” way to solve this?

Thanks 🙂

---

<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:** [December 2, 2018, 7:04pm UTC](https://discourse.processing.org/t/questions-on-static-methods/6169/2 "2018-12-02T19:04:21Z")

</div>

- You should know by now that all classes & interfaces inside “.pde” files are nested to a PApplet subclass.
- But when we declare them as `static` they behave pretty much like any other vanila top classes like those found in a “.java” file.
- B/c inner classes in “.pde” files belong to a PApplet subclass, we can access its members directly w/o using the dot `.` operator.
- However, when they’re declared as `static`, non-`static` members demand a PApplet reference.
- And if you really need to have `static` members inside your nested classes, those classes gotta be declared as `static` as well.
- Unless they’re merely `static` compile-time constants.
- If the above is the case, even inner classes can have those `static final` fields.
- But fret not! I’ve got an “elegant” workaround for ya. 😹

Do like Processing’s 3rd-party libraries: Request a PApplet argument as 1 of its constructor’s parameters: 💡

```java
static class MyVector {
  final PApplet p;
  float v1, v2;

  MyVector(final PApplet pa) {
    p = pa;
  }

  void randomize() {
    v1 = p.random(1);
    v2 = p.random(1);
  }
}

```

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [December 2, 2018, 7:14pm UTC](https://discourse.processing.org/t/questions-on-static-methods/6169/3 "2018-12-02T19:14:53Z")

</div>

Thank you for the answer 🙂

I would have something like this then:

```auto
static class MyVector {
  final PApplet pa;
  float v1, v2;

  MyVector(final PApplet pa, float v1, float v2) {
    this.v1 = v1;
    this.v2 = v2;
    this.pa = pa;
  }

  public static MyVector createUnitVector(final PApplet pa) {
    return new MyVector(pa, 1, 0);
  }
  
  public static MyVector createRandomVector(final PApplet pa) {
    return new MyVector(pa, pa.random(1), pa.random(1));
  }

  public void randomize() {
    v1 = pa.random(1);
    v2 = pa.random(1);
  }
}

```

---

<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:** [December 2, 2018, 7:31pm UTC](https://discourse.processing.org/t/questions-on-static-methods/6169/4 "2018-12-02T19:31:39Z")

</div>

If [**random()**](https://processing.org/reference/random_.html) is the only [PApplet](http://processing.github.io/processing-javadocs/everything/processing/core/PApplet.html) non-`static` member you need, you can replace it w/ Math.**random()**: 🤖  
[Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Math.html#random()](http://Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Math.html#random())

Or even Java’s Random class for more flexibility: ☕  
[Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/util/Random.html](http://Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/util/Random.html)

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [December 2, 2018, 7:33pm UTC](https://discourse.processing.org/t/questions-on-static-methods/6169/5 "2018-12-02T19:33:43Z")

</div>

Sure, it was just to illustrate the problem.

I was actually going to do so to fix it but wanted to know how to do otherwise!
