[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 (aka fields) 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 like
intorfloattypes only store 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 can use the new operator
and the class’s constructor, which is a special 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 georgeTheHorse = new Horse();
Now, georgeTheHorse is an object of type Horse.
If the constructor is defined with parameters, then when you call the
constructor you must provide arguments (values) for them. For example, if
the Horse constructor was defined to take a color as an argument:
Horse georgeTheHorse = new Horse(color(255, 0, 255));
Yes, that is a magenta Horse. In this example, Horse is the class name and
georgeTheHorse is our variable again, while color(255, 0, 255) (magenta) is
passed to the constructor as the sole argument to the constructor.
Separate Declaration and Instantiation¶
Like any variable in Processing, we can declare and instantiate object variables 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 georgeTheHorse; // Now the name is defined, // but the object is not yet created. void setup() { // Instantiation: georgeTheHorse = new Horse(); // Now the object has been created and can be used. }
Be careful! When the variable is first declared and before it is instantiated,
it holds null. Trying to access a property or method on that variable while
it is null will crash the program, because there is nothing to access!
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:
georgeTheHorse.coatColor = color(0, 0, 255); // change George's coatColor property
color c = georgeTheHorse.coatColor; // read it
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:
georgeTheHorse.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 = georgeTheHorse.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();
georgeTheHorse.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.”