# Creating a static variable from a non-static method (like color())

**URL:** https://discourse.processing.org/t/creating-a-static-variable-from-a-non-static-method-like-color/36641
**Category:** Coding Questions
**Created:** [April 28, 2022, 10:01pm UTC](https://discourse.processing.org/t/creating-a-static-variable-from-a-non-static-method-like-color/36641 "2022-04-28T22:01:41Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![GuyInFridge](https://avatars.discourse-cdn.com/v4/letter/g/2acd7d/32.png) [@GuyInFridge](https://discourse.processing.org/u/GuyInFridge)
#### Post date: [April 28, 2022, 10:01pm UTC](https://discourse.processing.org/t/creating-a-static-variable-from-a-non-static-method-like-color/36641/1 "2022-04-28T22:01:41Z")

</div>

I would like to create a class which has a static field “c\_DEFAULT” that stores a default color for all objects of that class. The problem is that I can’t do the following:

```nohighlight
static final color c_DEFAULT = color(175); // ERROR: Cannot make a static reference to the non-static method color() from the type PApplet

```

How should I go about this?

---

<div class="post-metadata">

### Author: ![PhySan](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/physan/32/16087_2.png) [@PhySan](https://discourse.processing.org/u/PhySan)
#### Post date: [April 28, 2022, 10:22pm UTC](https://discourse.processing.org/t/creating-a-static-variable-from-a-non-static-method-like-color/36641/2 "2022-04-28T22:22:13Z")

</div>

Hi @GuyInFridge ,

maybe try using final without the static 🙂  
[https://www.geeksforgeeks.org/final-keyword-in-java/](https://www.geeksforgeeks.org/final-keyword-in-java/)

---

<div class="post-metadata">

### Author: ![GuyInFridge](https://avatars.discourse-cdn.com/v4/letter/g/2acd7d/32.png) [@GuyInFridge](https://discourse.processing.org/u/GuyInFridge)
#### Post date: [April 29, 2022, 2:26am UTC](https://discourse.processing.org/t/creating-a-static-variable-from-a-non-static-method-like-color/36641/3 "2022-04-29T02:26:11Z")

</div>

@PhySan

I wanted to use a static variable because I have overloaded constructors of the following form:

```nohighlight
Class() {
    this(<<some default arguments here>>); // I want to use a default color variable here
}

// full constructor
Class(<<input parameters here>>) {
    // do something
}

```

And I want to say that if the user doesn’t pass in any parameters to the constructor, it should use some default values (including a default color). However, it is not possible to reference an instance variable when explicitly calling a constructor (I get an error). In other words, it’s only possible to reference a static variable where above I have written “some default arguments here.”

Is there another way I should go about doing this?

---

<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: [April 29, 2022, 5:19am UTC](https://discourse.processing.org/t/creating-a-static-variable-from-a-non-static-method-like-color/36641/4 "2022-04-29T05:19:46Z")

</div>

> [@GuyInFridge](#):
>
> `static final color c_DEFAULT = color(175); // ERROR:`

That’s a gray color value, so just use `175`. You don’t need the non-static function [**color()**](https://processing.org/reference/color_.html) for that:  
`static final color COLOR_DEFAULT = 175;`

These are some very old sketches that also use `static final color` inside a `class`:

1. “Talking Faces”: [sketchpad](http://studio.processingtogether.com/sp/pad/export/ro.9G5yFfYFFDXi1)
2. “Rain Game”: [sketchpad](http://studio.processingtogether.com/sp/pad/export/ro.9mzpXAdqbigsO)
3. “Player Move”: [sketchpad](http://studio.processingtogether.com/sp/pad/export/ro.91tcpPtI9LrXp)
4. “Wave Trios”: [sketchpad](http://studio.processingtogether.com/sp/pad/export/ro.9lTJ6qSlmCidk)
5. “Linked Balls”: [sketchpad](http://studio.processingtogether.com/sp/pad/export/ro.989GaZC5t7EkE)
6. “Asteroids Vs. Bullets Sim”: [sketchpad](http://studio.processingtogether.com/sp/pad/export/ro.9K-kjhuONcJDZ)

---

<div class="post-metadata">

### Author: ![GuyInFridge](https://avatars.discourse-cdn.com/v4/letter/g/2acd7d/32.png) [@GuyInFridge](https://discourse.processing.org/u/GuyInFridge)
#### Post date: [April 29, 2022, 1:02pm UTC](https://discourse.processing.org/t/creating-a-static-variable-from-a-non-static-method-like-color/36641/5 "2022-04-29T13:02:51Z")

</div>

@GoToLoop

I used color(175) just as an example, I was looking for the most general solution of saving a color generated using `color()`.

Looks like those sketches save the colors as Hex Codes instead, which works to set RGB values, but not alpha values to my understanding.

As an alternative, I know that the color() method generates a color which is secretly stored as an integer, so I guess I could try to use color() in a blank sketch like this:

```nohighlight
println(color(r,g,b,a)); // input whatever I want

```

copy the outputted integer, and then just directly save the integer into the static final color variable, and in that way I can use the color() method in whatever capacity I want (just not directly). For clarity within my class code, I can comment in what r/g/b/a values I used to generate the static color variable. So like this:

```nohighlight
static final color c = 840423333; // color(23,215,165,50)

```

I think the main thing I’ve learned here is that **I can’t _directly_ use a non-static method (e.g., color()) in the definition of a static variable, so there needs to be a workaround that doesn’t use a non-static method.**

But now I’m lost as to how to go about having other “default” properties for my classes that are more complex than color(), where I’m not sure what the workaround might look like. For example, I also wanted a default PShape for my class, that I would create with `createShape()` but that’s a non-static method, so it doesn’t work.

```nohighlight
// What I wish I could do... is there a workaround similar to what I did for color()?
static final PShape shape_DEFAULT = createShape(ELLIPSE,0,0,16,16);

```

Again, **I can’t use a non-static variable because I’m trying to pass these “default” properties into overloaded constructors which rely on each other. And I can’t use a static variable because the methods I’m using (e.g., color(), createShape()) aren’t static.**

Perhaps there’s some workaround to createShape() similar to color() where I can save the default value without needing to directly call the non-static method, but then that seems like an unsatisfactory solution, having to come up with workarounds for each new default thing I want that depends on a non-static method. It makes me think that I’m not properly organizing my code. Does anyone have other ideas?

---

<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: [April 29, 2022, 3:00pm UTC](https://discourse.processing.org/t/creating-a-static-variable-from-a-non-static-method-like-color/36641/6 "2022-04-29T15:00:43Z")

</div>

> [@GuyInFridge](#):
>
> Copy the outputted integer, and then just directly save the integer into the `static final color` variable,

For clarity use [**hex()**](https://processing.org/reference/hex_.html) after using [**color()**](https://processing.org/reference/color_.html) so you have a hex value representation: 🔆

```auto
final color c = color(23, 215, 165, 50);
println(c, TAB, "0x" + hex(c)); // 840423333 0x3217D7A5

```

> [@GuyInFridge](#):
>
> , where I’m not sure what the workaround might look like.

Best workaround I could come up w/ is to forward `null` values inside the intermediary signatures; and then leave the full signature to deal w/ those `null` values, by replacing them w/ the corresponding existing default values: 💡

```auto
// https://Discourse.Processing.org/t/
// creating-a-static-variable-from-a-non-static-method-like-color/36641/6

// GoToLoop (2022/Apr/29)

ColorShape cs;

void setup() {
  final color c = color(23, 215, 165, 50);
  println(c, TAB, "0x" + hex(c)); // 840423333 0x3217D7A5

  cs = new ColorShape();
  noLoop();
}

void draw() {
  background(0300);
  cs.display();
}

class ColorShape {
  static final color COLOR_DEFAULT = 0x3217D7A5; // color(23, 215, 165, 50)
  final PShape shape_default = createShape(ELLIPSE, 0, 0, 16, 16);

  color c;
  PShape shp;

  ColorShape() {
    this(COLOR_DEFAULT);
  }

  ColorShape(final color c) {
    this(c, null);
  }

  ColorShape(final PShape shp) {
    this(COLOR_DEFAULT, shp);
  }

  ColorShape(final color colour, final PShape shape) {
    c = colour;
    shp = shape != null? shape : shape_default;
  }

  ColorShape display() {
    shp.setFill(c);
    shape(shp, width >> 1, height >> 1);
    return this;
  }
}

```

---

<div class="post-metadata">

### Author: ![GuyInFridge](https://avatars.discourse-cdn.com/v4/letter/g/2acd7d/32.png) [@GuyInFridge](https://discourse.processing.org/u/GuyInFridge)
#### Post date: [April 29, 2022, 4:02pm UTC](https://discourse.processing.org/t/creating-a-static-variable-from-a-non-static-method-like-color/36641/7 "2022-04-29T16:02:15Z")

</div>

Did not know you could use hex() to represent RGB and RGBA values! And clever solution with the `null`. Thanks! Hopefully any other “default” values that I want to add can be written in some static way or maybe I can pass null or some “default” value through the overloaded constructors then deal with assigning default values in the primary constructor.
