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

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:

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?

Hi @GuyInFridge ,

maybe try using final without the static :slight_smile:
https://www.geeksforgeeks.org/final-keyword-in-java/

@PhySan

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

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?

That’s a gray color value, so just use 175. You don’t need the non-static function color() 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
  2. “Rain Game”: sketchpad
  3. “Player Move”: sketchpad
  4. “Wave Trios”: sketchpad
  5. “Linked Balls”: sketchpad
  6. “Asteroids Vs. Bullets Sim”: sketchpad
1 Like

@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:

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:

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.

// 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?

For clarity use hex() after using color() so you have a hex value representation: :high_brightness:

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

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: :bulb:

// 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;
  }
}
1 Like

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.

1 Like