j.a456 j.a456 - 6 months ago 9
Java Question

Class variable or object?

If a program includes two classes named

Terrain
and
Simulation
and inside the
Terrain
class is the line:

private static Simulation sim;


Is that similar to creating a new object of the simulation class which can be achieved with the following syntax:

Simulation sim = new Simulation();


Or does it mean that
sim
is a variable of class type
Simulation
?

public class terrain {
private static Simulation sim;

public static void main(String[] args) {}
}

public class Simulation {}

Answer
private static Simulation sim;

That's a declaration of static (class) variable of Simulation type inside your terrain class.

Does it mean that sim is a variable of class type Simulation?

Yes, indeed.

Is that similar to creating a new object of the simulation class...?

No, you simply declared it, didn't initialize. I initialized it directly after the declaration:

private static Simulation sim = new Simulation();
Comments