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

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

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

  • 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.

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

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

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

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

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

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:

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

Thank you very much, appreciate !

1 Like