[This guide specifically addresses the syntax of objects in Processing, but the ideas and even much of the syntax apply to objects in many other languages as well.]
This guide serves as a quick introduction and a quick reference for the syntax and semantics of objects in Processing. For a bit more discussion of the benefit of using objects and related concerns, see the Processing tutorial on objects.
Basic Ideas¶
- An object can contain data and code, known as properties and methods, respectively, all bundled together in one unit.
- Properties are data: variables kept inside the object.
- Methods are code: functions kept inside the object.
- Every property and method has its own name.
- We use variables to store and refer to objects, but whereas non-object variables only refer to a single value, an object can contain multiple values (properties) and functions (methods).
- A class is like a blueprint or cookie-cutter that defines a particular type of object. From one class, we can create many objects of that type.
Creating an Object¶
To create an object from a class in Processing, you have to use the new
operator and the class’s constructor, which is a function with the same
name as the class.
The pattern for creating a new object is:
<class name> <object name> = new <class name>(<arguments>);
The left hand side of the assignment is the same as other variable declarations in Processing: <type> <name>
An object variable’s data type is just the class name.
For example, if there is a class called Horse
, we can make a new Horse
object:
Horse TomTheHorse = new Horse();
Now, TomTheHorse
is an object of type Horse
.
Alternatively, we can declare and instantiate the object variable separately. The “declaration” is simply telling Processing that the object variable exists and what type it has, while the “instantiation” is what actually creates a new object to be stored in that variable.
This is useful, for example, when you want to declare a global variable
(declaring it outside of any function) and instantiate it in the setup()
function:
// Declaration Horse TomTheHorse; // Now the name is defined, // but the object is not yet created. void setup() { // Instantiation TomTheHorse = new Horse(); // Now the object has been created and can be used. }
If the constructor is defined with parameters, put arguments for them in
the call to the constructor. For example, if the Horse
constructor takes a
color
as an argument:
Horse TomTheHorse = new Horse(color(255, 0, 255));
Yes, that is a magenta Horse
. In this example, Horse
is the class name and
TomTheHorse
is our variable again, while color(255, 0, 255)
is passed to
the constructor as the sole argument to the constructor.
Accessing Properties in an Object¶
To access a named property of an object, we use dot notation.
The basic pattern of dot notation is the same in many languages, including Processing, Java, C++, and Python:
<object name>.<property name>
This means “the property named <property name> inside the object named <object name>.”
The object name is given first, on the left, and the property name is second, on the right. Dot notation always accesses the name on the right inside the name on the left.
A property can be used like any other variable. You can assign a value to it by placing it on the left side of an assignment statement, or you can use its value in any expression like you would any variable.
Calling Methods in an Object¶
Remember: objects contain not only data, but methods (functions) also. To call a method that is part of a particular object, we use dot notation again.
The basic pattern for calling a method inside a given object is:
<object name>.<method name>(<arguments>);
For example:
TomTheHorse.gallopAround();
or, if the method returns a value, you can save it into a newly declared variable, just like a regular function’s return value:
int age = TomTheHorse.getAge();
or, if we have a HorseFood
class and an eatFood()
method in the Horse
object that takes a HorseFood
object as an argument:
HorseFood grub = new HorseFood();
TomTheHorse.eatFood(grub);
You’ll see plenty of examples of these patterns in any code that uses objects. The key idea to remember is dot notation and what it means. Always remember: “x.y means: access the thing named y inside the thing named x.”