Need to run something after declaring an object

I’m currently writing a bit of code I want to reuse in other projects. It’s a GUI framework. The problem is that I don’t want to constantly keep typing in a specific line of code every single time I start using whatever feature I need from each function. The solution was simple enough, I created an update function(stub), but now I need to run a function to add the new object to the code automatically upon the creation of the object. How would I achieve this? I want to do this in JuiceUI, and not JuiceUI_Example The code is below, albeit quite poorly made.

JuiceUI_Example(main file)

JuiceUI example = new JuiceUI();
JuiceUI.button ButtonA = example.new button();

void setup() {
 size(640,480);
 surface.setTitle("JuiceUI Testing Program!");
 surface.setResizable(true);
 surface.setLocation(100, 100);
 frameRate(60);
 //Button A setup
 
}

void draw() {
  ButtonA.disable = false;
  if (frameCount%30==0) {
  example.update(); 
  print("ButtonA", ButtonA.disable, "\n");
  }
}

JuiceUI(the framework I’m writing)

public class JuiceUI {
  void update() {
  print("Update has been called at frame", frameCount, "\n");
  }

//Button manipulation
  public class button {
  public float posx = 0;
  public float posy = 0;
  int sizex = 100;
  int sizey = 50;
  String text;

  boolean disable = false;
  byte status = -127;
  void autorun() {
    
  }
  }
}
1 Like

I can answer this one myself. This one was ridiculously easy, and a clear example of why you should do research before going from imperative to object orientated. It’s called a constructor, and looks a bit like this.

//Button manipulation
  public class button {
. . .
  button() {
    print("New button created.");
  }
1 Like

Welcome to the forum. All questions, comments and findings are welcome. It’s all about learning and sharing. Sometimes you need to stop and thing to see the answer, like you did :grin: