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

**URL:** <https://discourse.processing.org/t/need-to-obtain-object-name-in-constructor-solution-referencing/25039>\
**Category:** Coding Questions\
**Created:** [October 30, 2020, 2:12am UTC](https://discourse.processing.org/t/need-to-obtain-object-name-in-constructor-solution-referencing/25039 "2020-10-30T02:12:46Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![xX\_pokeman2003\_Xx](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/xx_pokeman2003_xx/32/10962_2.png) [@xX\_pokeman2003\_Xx](https://discourse.processing.org/u/xX_pokeman2003_Xx)\
**Post date:** [October 30, 2020, 2:12am UTC](https://discourse.processing.org/t/need-to-obtain-object-name-in-constructor-solution-referencing/25039/1 "2020-10-30T02:12:46Z")

</div>

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.

```auto
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.

```auto
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!

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [October 30, 2020, 2:27am UTC](https://discourse.processing.org/t/need-to-obtain-object-name-in-constructor-solution-referencing/25039/2 "2020-10-30T02:27:47Z")

</div>

There is a nice tutorial about objects on the website

Did you see?

---

<div class="post-metadata">

**Author:** ![xX\_pokeman2003\_Xx](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/xx_pokeman2003_xx/32/10962_2.png) [@xX\_pokeman2003\_Xx](https://discourse.processing.org/u/xX_pokeman2003_Xx)\
**Post date:** [October 30, 2020, 2:59am UTC](https://discourse.processing.org/t/need-to-obtain-object-name-in-constructor-solution-referencing/25039/3 "2020-10-30T02:59:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [October 30, 2020, 8:14am UTC](https://discourse.processing.org/t/need-to-obtain-object-name-in-constructor-solution-referencing/25039/4 "2020-10-30T08:14:35Z")

</div>

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();

---

<div class="post-metadata">

**Author:** ![xX\_pokeman2003\_Xx](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/xx_pokeman2003_xx/32/10962_2.png) [@xX\_pokeman2003\_Xx](https://discourse.processing.org/u/xX_pokeman2003_Xx)\
**Post date:** [October 30, 2020, 8:37am UTC](https://discourse.processing.org/t/need-to-obtain-object-name-in-constructor-solution-referencing/25039/5 "2020-10-30T08:37:07Z")

</div>

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

```auto
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

```auto
//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();
  }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [October 30, 2020, 8:42am UTC](https://discourse.processing.org/t/need-to-obtain-object-name-in-constructor-solution-referencing/25039/6 "2020-10-30T08:42:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [October 30, 2020, 8:43am UTC](https://discourse.processing.org/t/need-to-obtain-object-name-in-constructor-solution-referencing/25039/7 "2020-10-30T08:43:19Z")

</div>

> [@xX\_pokeman2003\_Xx](#):
>
> `public class button {`

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

---

<div class="post-metadata">

**Author:** ![xX\_pokeman2003\_Xx](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/xx_pokeman2003_xx/32/10962_2.png) [@xX\_pokeman2003\_Xx](https://discourse.processing.org/u/xX_pokeman2003_Xx)\
**Post date:** [October 30, 2020, 8:45am UTC](https://discourse.processing.org/t/need-to-obtain-object-name-in-constructor-solution-referencing/25039/8 "2020-10-30T08:45:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![xX\_pokeman2003\_Xx](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/xx_pokeman2003_xx/32/10962_2.png) [@xX\_pokeman2003\_Xx](https://discourse.processing.org/u/xX_pokeman2003_Xx)\
**Post date:** [October 30, 2020, 8:59pm UTC](https://discourse.processing.org/t/need-to-obtain-object-name-in-constructor-solution-referencing/25039/9 "2020-10-30T20:59:46Z")

</div>

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.

```auto
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?

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [October 30, 2020, 9:10pm UTC](https://discourse.processing.org/t/need-to-obtain-object-name-in-constructor-solution-referencing/25039/10 "2020-10-30T21:10:24Z")

</div>

```auto

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

```

---

<div class="post-metadata">

**Author:** ![xX\_pokeman2003\_Xx](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/xx_pokeman2003_xx/32/10962_2.png) [@xX\_pokeman2003\_Xx](https://discourse.processing.org/u/xX_pokeman2003_Xx)\
**Post date:** [October 30, 2020, 9:13pm UTC](https://discourse.processing.org/t/need-to-obtain-object-name-in-constructor-solution-referencing/25039/11 "2020-10-30T21:13:29Z")

</div>

Perfect, thank you! It works!
