# How to access all variables of a single class?

**URL:** https://discourse.processing.org/t/how-to-access-all-variables-of-a-single-class/5409
**Category:** Coding Questions
**Created:** [November 10, 2018, 6:50pm UTC](https://discourse.processing.org/t/how-to-access-all-variables-of-a-single-class/5409 "2018-11-10T18:50:43Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Architector\_4](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/architector_4/32/813_2.png) [@Architector\_4](https://discourse.processing.org/u/Architector_4)
#### Post date: [November 10, 2018, 6:50pm UTC](https://discourse.processing.org/t/how-to-access-all-variables-of-a-single-class/5409/1 "2018-11-10T18:50:43Z")

</div>

Say, for example, I have this class:

```auto
class thing{
  float x;
  float y;
  String z;
  String w;
  boolean omega;
  int foo;
  thing(){} //A meh constructor, I know. ._.
}

```

And I want to repeat a specific set of code on all float variables in the class, and then another with all string variables.  
One thing I can do for this is simply manually repeat that piece of code for every float and every string, considering that I know all of them: `doThing(thatThing.x); doThing(thatThing.y); doStringThing(thatThing.z); ...`  
But, this means that if I’ll add or remove variables from this class, I’ll also have to edit this line of `doThing()` calls to fit the new contents. And I don’t like repeating stuff in this fashion, so I’m wondering - is there a way to do this any better?

One way that I did consider is making a 1 row long `Table` instead of a new class, store everything inside of that, and then use `getColumnTitles()` to get a list of all “variables” in there - however that is significantly slower than having these variables the normal way. Or a `JSONObject` using the same methods - except it doesn’t have `getColumnTitles()` and has an iterator instead - but with this usage it’s almost the same thing and still shows poor performance.

Any ideas?

---

<div class="post-metadata">

### Author: ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)
#### Post date: [November 11, 2018, 2:05am UTC](https://discourse.processing.org/t/how-to-access-all-variables-of-a-single-class/5409/2 "2018-11-11T02:05:19Z")

</div>

I think you need to provide some code. I do not get your question with the information you provided.

Kf

---

<div class="post-metadata">

### Author: ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)
#### Post date: [November 11, 2018, 9:45am UTC](https://discourse.processing.org/t/how-to-access-all-variables-of-a-single-class/5409/3 "2018-11-11T09:45:14Z")

</div>

You have a class ‚Thing‘ with 10 int variables and 5 Strings and some others. Now he wants to know if there is a way to access all ints because he doesn‘t want to changed them manually When he changes them inside the class. Like

```auto
((Thing)thing).getAllInts(); //returns all int variables in the class. 

```

Oh and he probably doesn‘t want to have it as a new method like

```auto
int[] getAllInts() {
  int[] ret = new Int[10];
  ret = append..... and then just append each int
  return ret;
}

```

because Then he has to edit this method, which is the Same as just editing the normal code…

---

<div class="post-metadata">

### Author: ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)
#### Post date: [November 11, 2018, 9:50am UTC](https://discourse.processing.org/t/how-to-access-all-variables-of-a-single-class/5409/4 "2018-11-11T09:50:57Z")

</div>

Well, you could group all your variables by type.

```auto
class thing{
  float[] xy = new float[2]; // xy[0] is x, xy[1] is y
  String[] zw = new String[2];
  boolean omega;
  int foo;
  thing(){} //A meh constructor, I know. ._.
  void doThingForAllFloat(){
    for( int i = 0; i < xy.length; i++){
      xy[i] = do_thing( xy[i] );
    }
  } 
}

```

Does that make any sense? It’s actually more confusing if you ask me…

---

<div class="post-metadata">

### Author: ![Architector\_4](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/architector_4/32/813_2.png) [@Architector\_4](https://discourse.processing.org/u/Architector_4)
#### Post date: [November 11, 2018, 11:12am UTC](https://discourse.processing.org/t/how-to-access-all-variables-of-a-single-class/5409/5 "2018-11-11T11:12:57Z")

</div>

It does make sense, and it pretty much answers the question - however it also means dealing with arrays, and remembering what is what in those arrays, so I’ll just stick to manual repeating, actually. I think I’ll remember to change everything I need if I change the class’s insides.

Thanks for the responses though!

---

<div class="post-metadata">

### Author: ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)
#### Post date: [November 11, 2018, 11:14am UTC](https://discourse.processing.org/t/how-to-access-all-variables-of-a-single-class/5409/6 "2018-11-11T11:14:56Z")

</div>

Sorry, there is just no practical way to do it. Though some Programms do this automatically, like eclipse.
