Need to obtain object name in constructor(Solution:Referencing)

For transparency sake, I left the original post here, but the solution for the actual question itself is pretty simple:You can’t. HOWEVER, there is a work around that lets you do something similar, by referencing the object.

Hi all! Second post after my stupid first post. I have a fairly simple question. I need to somehow obtain the new object name within the constructor to add it to an array. How would I achieve this?

For help visualizing, here’s what I need to happen.

TestExample example = new examplemain();
TestExample.element exelement = example.new element();

At this point, with the creation of the new button object, I have a constructor. The examplemain class has a function known as update. I need this update to run through every single object to update drawing elements associated with exelement. The most obvious way is to add it to an array, and then when example.update() is called, just run an array to update everything. Something like this WOULD achieve the effect I’m looking for, but only for exelement, which is no good.

element() {
TestExample.array[0]="exelement"
}

I found things like getName() or getClass(), but I couldn’t find any meaningful way to apply it in the method I need.

Thanks in advance!

There is a nice tutorial about objects on the website

Did you see?

Did you see?

I couldn’t find it, probably because I’m really, REALLY blind. Do you mind linking to it?
Edit:I found it but it doesn’t seem to provide what I’m looking for. Do you know exactly what part I should be looking at?

Please provide the entire sketch

before setup declare array

Fish[] fishs = new Fish [3];

in setup:

fishs[0]= new Fish();
fishs[1]=…

in draw fishs[0].display();

or a for loop

for (Fish f : fishs)
f.display();

The entire sketch? Fine. There’s not really much of worth though. I just need to somehow pass ButtonA to the construct. Alternatively(although I don’t want to try this), I could just somehow reference it with this, but I haven’t found a way to manipulate things through a reference like that.
JuiceUI_Example

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() {  
  if (frameCount%30==0) example.update();
  example.list[0] = null;
  print(ButtonA.status, example.list[0]);
}

JuiceUI

//Version 1
public class JuiceUI {
  String[] list = new String[3];
  void update() {
  print("Update has been called at frame", frameCount, "\n");
  }

//Button manipulation
  public class button {
  float posx = 0;
  float posy = 0;
  int sizex = 100;
  int sizey = 50;
  String text;
  boolean disable = false;
  byte status = -127;
  button() {
    print("New button created.", this, "\n");
    list[0] = this.toString();
  }
  }
}

Maybe I begin to understand what you mean.

The way I see it, when you have multiple buttons in the UI, why not make an ArrayList Inside the class and a method addButton

Note that there are lots of gui libraries around on the website

It’s a convenient convention that class names start with a Capital, so Button

1 Like

I know but I am making a “simple” GUI framework for myself, mostly for the sake of practice. Thanks for the tip though.
I could potentially do the addButton method, but I was planning to try to keep it closer to the Win32API standards, where each button was its own object. However, it seems I might have to ditch this convention, which in that case, no worries, just disappointing.

It’s like 2 AM for me, I have to get sleep, I’ll continue this thread in the morning.

1 Like

Alright, I am good to go. I could potentially REFERENCE the object rather than obtaining the name. That should be easier, but the method someone else gave me is simply throwing a “Class ‘List’ does not exist” error. The solution they gave me looks like this, but it wasn’t for Processing, just for regular Java, so I have I no idea what differences there are.

public class Outer {
    private List<Element> allElements = new ArrayList<>();

    public class Element {
        
        public Element() {
            ...
            allElements.add(this);
        }

        public void update() { ... } 
    }

    public void updateAll() {
        for (Element e in allElements) {
            e.update();
        }
    }
}

How would I adapt this code?




void setup() {
  size(660, 700);
}

void draw() {
}

// ====================================================================

class UI {

  ArrayList<Element> elements = new ArrayList();

  void update() {
    for (Element e : elements) {
      e.update();
    }
  }//method
  //
}//class 

// ====================================================================

class Element {

  // constr
  Element() {
    // constr
  }// constr

  public void update() { 
    //
  }//method
  //
}//class
//
1 Like

Perfect, thank you! It works!

1 Like