# Trying to modifiy a variable in a class and access it from a subclass

**URL:** <https://discourse.processing.org/t/trying-to-modifiy-a-variable-in-a-class-and-access-it-from-a-subclass/38691>\
**Category:** Beginners\
**Created:** [September 9, 2022, 12:08pm UTC](https://discourse.processing.org/t/trying-to-modifiy-a-variable-in-a-class-and-access-it-from-a-subclass/38691 "2022-09-09T12:08:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![xfred53](https://avatars.discourse-cdn.com/v4/letter/x/58956e/32.png) [@xfred53](https://discourse.processing.org/u/xfred53)\
**Post date:** [September 9, 2022, 12:08pm UTC](https://discourse.processing.org/t/trying-to-modifiy-a-variable-in-a-class-and-access-it-from-a-subclass/38691/1 "2022-09-09T12:08:44Z")

</div>

Hi,  
A beginner question for sure but cannot solve it all alone.  
I have a first class (One) with a single field that is initiated with its constructor.  
I can access it from method (display) of this class.  
I then have a second class (Two) that extends the first one. I can access the field of the first one by a method (display) of the second class.  
Everything ok.  
Now I modify this field with a method of the first class (update) and I can access it with the display method.  
But when accessing this field from the second class, I still have the old value ???

```auto
One a;
Two b;

void setup() {
  a=new One();
  b=new Two();
  
  a.display();
  b.display();
  
  a.update();
  a.display();
  b.display();
}

void draw() {}

public class One {
  int num;
  One() {
    num=9;
  }
  void display() {
    println("from One: "+num);
  }
  void update() {
    num++;
  }    
}

class Two extends One{
  Two() {
  }
  void display() {
    println("from Two "+num);
  }  
}

```

Output is :  
from One: 9  
from Two 9  
from One: 10  
from Two 9 ???

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [September 9, 2022, 12:24pm UTC](https://discourse.processing.org/t/trying-to-modifiy-a-variable-in-a-class-and-access-it-from-a-subclass/38691/2 "2022-09-09T12:24:38Z")

</div>

> [@xfred53](#):
>
> But when accessing this field from the second class, I still have the old value ???

- Each `new` object got its own copy of its _class_ fields.
- So the object _a_ has its own field _num_ & object _b_ has its own field _num_.
- Calling method **update()** over object _a_ will only update _a_’s _num_, not _b_’s _num_.

---

<div class="post-metadata">

**Author:** ![xfred53](https://avatars.discourse-cdn.com/v4/letter/x/58956e/32.png) [@xfred53](https://discourse.processing.org/u/xfred53)\
**Post date:** [September 9, 2022, 12:56pm UTC](https://discourse.processing.org/t/trying-to-modifiy-a-variable-in-a-class-and-access-it-from-a-subclass/38691/3 "2022-09-09T12:56:15Z")

</div>

Thanks for the answer.  
Is it a way to dynamically share variables between instances of a same class ?

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [September 9, 2022, 1:14pm UTC](https://discourse.processing.org/t/trying-to-modifiy-a-variable-in-a-class-and-access-it-from-a-subclass/38691/4 "2022-09-09T13:14:00Z")

</div>

Yes through class attributes . This sketch keeps track of the number of instances created form class One and its child classes.

```auto
One a;
Two b;

void setup() {
  a = new One();
  b = new Two();
  println("Initial conditions");
  a.display();
  b.display();
  // Updatre One twice
  println("After aX2 update and bX1 update");
  a.update();
  a.update();
  a.display();
  // Update Two once
  b.update();
  b.display();
}

void draw() {
}

static public class One {
  static int nbrInstances = 0;
  int num;
  One() {
    nbrInstances++;
    num=9;
  }
  void display() {
    print(this.getClass().getSimpleName());
    println(" has Values = " + num + " nbr of objects" + nbrInstances);
  }

  void update() {
    num++;
  }
}

static class Two extends One {
  Two() {
  }
}

```

The output is

```auto
Initial conditions
One has Values = 9 nbr of objects2
Two has Values = 9 nbr of objects2
After aX2 update and bX1 update
One has Values = 11 nbr of objects2
Two has Values = 10 nbr of objects2

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [September 9, 2022, 1:27pm UTC](https://discourse.processing.org/t/trying-to-modifiy-a-variable-in-a-class-and-access-it-from-a-subclass/38691/5 "2022-09-09T13:27:30Z")

</div>

> [@xfred53](#):
>
> Is it a way to dynamically share variables between instances of the same class?

As @quark already got ahead, just have `static` fields inside a `static class`:

> **[static / Reference](https://processing.org/reference/static.html)**
>
> Keyword used to define a variable as a "class variable" and a method as a "class method." When a variable is declared with the static keyword, all instances of that class share the same variabl…

However, if you’re somehow impeded to declare a `class` as `static`, just create an extra  
`static class` and `extends` it, so its subclasses are free to continue being non-`static`:

```auto
// https://Discourse.Processing.org/t/
// trying-to-modifiy-a-variable-in-a-class-and-access-it-from-a-subclass/38691/5

// (2022-Sep-09)

final One a = new One();
final Two b = new Two();

void setup() {
  println(a, TAB, b); // One: 9 Two: 9

  a.update();
  println(a, TAB, b); // One: 10 Two: 10

  b.update();
  println(a, TAB, b); // One: 11 Two: 11

  exit();
}

static abstract class Zero {
  static int num;
}

class One extends Zero {
  One() {
    num = 9;
  }

  void update() {
    ++num;
  }

  String toString() {
    return getClass().getSimpleName() + ": " + num;
  }
}

class Two extends One {
}

```

---

<div class="post-metadata">

**Author:** ![xfred53](https://avatars.discourse-cdn.com/v4/letter/x/58956e/32.png) [@xfred53](https://discourse.processing.org/u/xfred53)\
**Post date:** [September 9, 2022, 1:27pm UTC](https://discourse.processing.org/t/trying-to-modifiy-a-variable-in-a-class-and-access-it-from-a-subclass/38691/6 "2022-09-09T13:27:36Z")

</div>

Thank you very much, appreciate !
