Separate objects in an ArrayList

first of all, this might be interesting to you: https://processing.org/examples/variablescope.html

depending on where you declare your variables they can be accessed (or not) from different methods. everything outside setup() and draw() will be visible from everywhere (aka global variable).

values you declare inside your class “belong” to it, since they are only accessible from there. (more or less :-))

a class would look like this:

class Tegenstander {
//--------------------- variables "inside" the class' {}
float x;
float y;
//--------------------- a constructor - used to initialize an instance of the class
  Tegenstander () {
  }
//--------------------- class methods
  void update() {
  }
}

as you already have done, you declare that there IS an object of this class first
Tegenstander someName;. similar to saying: int a;

then you actually “instantiate” your object: someName = new Tegenstander();
(or in your case even make a list of many of these, as you already have done.

so every object you create has the methods and the variables at its disposal that you have described inside your class{}

typically, from the “inside” your methods can happily use your variables:

void update () {
x = 232134239; 
// calculate wonderful stuff here, change things
}

from the outside you cannot access the variables, unless you make specific methods for it (I hear that’s good practice…)

int getX () {
return x;
}

so you might say (for example in draw())

int a = someName.getX(); retrieving the value from the object or, if you have many instances (e.g. in an array) int a = someName[n].getX(); where you have to know the correct object you want to address.

just as you did with calling the display() method in your code with t.display()

finally you can access the field (that’s what that variable is usually called) directly by writing:
int a = nyName.x;
or change it
myName.x = 777;

3 Likes