# Get class variables from another class

**URL:** <https://discourse.processing.org/t/get-class-variables-from-another-class/14866>\
**Category:** Beginners\
**Created:** [October 21, 2019, 10:29pm UTC](https://discourse.processing.org/t/get-class-variables-from-another-class/14866 "2019-10-21T22:29:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![HoboGreed](https://avatars.discourse-cdn.com/v4/letter/h/da6949/32.png) [@HoboGreed](https://discourse.processing.org/u/HoboGreed)\
**Post date:** [October 21, 2019, 10:29pm UTC](https://discourse.processing.org/t/get-class-variables-from-another-class/14866/1 "2019-10-21T22:29:44Z")

</div>

How do I get a variable from the class that created the class ?

```auto
One a;

void setup() {
  a=new One();
}
void draw() {}

public class One {
  Two b;
  int num;

  One() {
    num=9;
    println(num);
    b=new Two();
  }
}
class Two {
  Two() {
    println(a.num); // error How do I get the num variable ?
  }
}

```

---

<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 21, 2019, 10:34pm UTC](https://discourse.processing.org/t/get-class-variables-from-another-class/14866/2 "2019-10-21T22:34:15Z")

</div>

Is this a trick question?

I think in the above code the issue is that you try use a before a‘s constructor is finished

The println line itself is okay

---

<div class="post-metadata">

**Author:** ![HoboGreed](https://avatars.discourse-cdn.com/v4/letter/h/da6949/32.png) [@HoboGreed](https://discourse.processing.org/u/HoboGreed)\
**Post date:** [October 21, 2019, 11:26pm UTC](https://discourse.processing.org/t/get-class-variables-from-another-class/14866/3 "2019-10-21T23:26:03Z")

</div>

Not a trick question. Simply asking how to get a variable from another class.

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [October 22, 2019, 2:45am UTC](https://discourse.processing.org/t/get-class-variables-from-another-class/14866/4 "2019-10-22T02:45:30Z")

</div>

hm, possibly you wanted this?

```auto
//One a;
Two b;

void setup() {
// a=new One();
  b=new Two();
}

void draw() {}

public class One {
// Two b;
  int num;
  One() {
    num=9;
    println("from One: "+num);
// b=new Two();
  }
}

class Two extends One{
  Two() {
    println("from Two b "+num);
  }
}
// not even need super(..)

```

---

<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 22, 2019, 6:11am UTC](https://discourse.processing.org/t/get-class-variables-from-another-class/14866/5 "2019-10-22T06:11:14Z")

</div>

> [@HoboGreed](#):
>
> println(a.num);

I think that’s the right way

The reason why it didn’t work is that you use it in the constructors

I didn’t mean to be rude

---

<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 22, 2019, 8:14am UTC](https://discourse.processing.org/t/get-class-variables-from-another-class/14866/6 "2019-10-22T08:14:49Z")

</div>

here

this shows that you can access a value from another class / object via `println(a.num);`

As I explained, the issue was outside this line.

Note that `a` is a global object and therefore known inside class Two

```auto

One a;

void setup() {
  a = new One();
}

void draw() {
  a.display();
}

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

public class One {
  Two b;
  int num;

  One() {
    //constr
    num=9;
    println(num);
    b=new Two();
  }

  void display() {
    b.display();
  }
}//class

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

class Two {
  Two() {
    //constr
  }

  void display() {
    println(a.num); // no error
  }
}//class
//

```

---

<div class="post-metadata">

**Author:** ![HoboGreed](https://avatars.discourse-cdn.com/v4/letter/h/da6949/32.png) [@HoboGreed](https://discourse.processing.org/u/HoboGreed)\
**Post date:** [October 23, 2019, 5:23pm UTC](https://discourse.processing.org/t/get-class-variables-from-another-class/14866/7 "2019-10-23T17:23:41Z")

</div>

No, you weren’t rude. Thanks for the help
