# Classes initialising other classes

**URL:** https://discourse.processing.org/t/classes-initialising-other-classes/13768
**Category:** Beginners
**Created:** [September 2, 2019, 4:46pm UTC](https://discourse.processing.org/t/classes-initialising-other-classes/13768 "2019-09-02T16:46:04Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![arcadeperfect](https://avatars.discourse-cdn.com/v4/letter/a/7ab992/32.png) [@arcadeperfect](https://discourse.processing.org/u/arcadeperfect)
#### Post date: [September 2, 2019, 4:46pm UTC](https://discourse.processing.org/t/classes-initialising-other-classes/13768/1 "2019-09-02T16:46:04Z")

</div>

Hello!

This is more a Java question than processing but I’m hoping the processing folks will be more merciful that stack overflow 🙂 I am trying to translate [this](https://www.thanassis.space/gravity.html) gravity simulator from python to processing.

I’m familiar with python and understand the program syntactically (if not mathematically) but I’m struggling with the Java syntax.

Below code is my attempt at a small chunk of the program. The first class is just to hold the state of each planet, the second class is the planet and should initiate an instance of the State class.

When the State class gets initiated in the Planet class’s constructor, it returns a Null when I try to print it. What am I doing wrong? (I expect lots…)

```auto
// not allowed globals in java
// so i put in the planet class

// int WIDTH = 900;
// int HEIGHT = 600;
// int PLANETS = 30;
// float DENSITY = 0.01;
// float GRAVITYSTRENGTH = 1.e4;

class State {

	//Class representing position and velocity.

	float x;
	float y;
	float vx;
	float vy;

	public State(float _x, float _y, float _vx, float _vy){
		println("init state");
		x = _x;
		y = _y;
		vx = _vx;
		vy = _vy;
	}
}

class Planet{

	/* Class representing a planet. The "_st" member is an instance of "State",
    carrying the planet's position and velocity - while the "_m" and "_r"
    members represents the planet's mass and radius. */

	int WIDTH = 900;
	int HEIGHT = 600;
	int PLANETS = 30;
	float DENSITY = 0.01;
	float GRAVITYSTRENGTH = 1.e4;
	ArrayList planets;
	State _st;

    public Planet(){
		//planets = listOfPlanets;
		println("init planet");
		State _st = new State(
			float(int(random(0, WIDTH))), 
			float(int(random(0, HEIGHT))), 
			((random(0,300) / 100.0) - 1.5),
			((random(0,300) / 100.0) - 1.5));
		println(_st.x, _st.y);	// printing state's variables works here

	    float _r = 1.5;
	    boolean _merged = false;

	}
}

// test instance of State works
State testState = new State(1,2,3,4);
println(testState.x,testState.y); // works

// instance of planet 
Planet sun = new Planet();
println(sun._st); // returns Null

```

Thankyou!

---

<div class="post-metadata">

### Author: ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)
#### Post date: [September 2, 2019, 4:49pm UTC](https://discourse.processing.org/t/classes-initialising-other-classes/13768/2 "2019-09-02T16:49:46Z")

</div>

Hi,

I think it is because you are creating a local variable \_st inside the Planet constructor.

Try simply write:

```auto
_st = new State(
			float(int(random(0, WIDTH))), 
			float(int(random(0, HEIGHT))), 
			((random(0,300) / 100.0) - 1.5),
			((random(0,300) / 100.0) - 1.5));

```

---

<div class="post-metadata">

### Author: ![arcadeperfect](https://avatars.discourse-cdn.com/v4/letter/a/7ab992/32.png) [@arcadeperfect](https://discourse.processing.org/u/arcadeperfect)
#### Post date: [September 2, 2019, 4:51pm UTC](https://discourse.processing.org/t/classes-initialising-other-classes/13768/3 "2019-09-02T16:51:14Z")

</div>

oh yeah that worked!

thanks!
