04.04.2013 Views

Processing: Creative Coding and Computational Art

Processing: Creative Coding and Computational Art

Processing: Creative Coding and Computational Art

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

PROCESSING: CREATIVE CODING AND COMPUTATIONAL ART<br />

308<br />

// go to next line after printing<br />

// cooking instructions to the screen<br />

println();<br />

}<br />

}<br />

Class declaration<br />

class BurritoRecipe {<br />

As I discussed, to declare a class, you use the class keyword followed by the identifier<br />

(BurritoRecipe) of the class. Following the class identifier, there needs to be an open<br />

curly brace.<br />

Properties declaration<br />

// properties<br />

int size;<br />

String tortillaFlavor;<br />

String meatType;<br />

String beanType;<br />

String[]toppings;<br />

int salsaTemperature;<br />

Properties are declared at the top part of the class. As in previous examples with variables,<br />

properties need a data type <strong>and</strong> a legal identifier to be declared; they can also be assigned<br />

initial values. In this example, I just declare the properties. Notice that of the six properties,<br />

two are of type int, three are of type String, <strong>and</strong> one is of type String[] (array of<br />

Strings). The six declared properties are technically known as instance properties, <strong>and</strong><br />

each object created (or instantiated) from the BurritoRecipe class will have access to its<br />

own unique copies of these properties. This is an important point <strong>and</strong> illustrates the blueprint<br />

nature of classes. The properties <strong>and</strong> methods defined in the class are the core structural<br />

elements (commonly referred to as members) of each object instantiated from the<br />

class, <strong>and</strong> each object can express itself uniquely via its own copies of the class’s properties<br />

<strong>and</strong> methods.<br />

In addition to instance properties, classes can also have static properties <strong>and</strong> static methods.<br />

These are declared with the keyword static. Static properties are also known as class<br />

properties. I will provide an example of some static variables when I cover composition<br />

shortly. The main difference between instance properties <strong>and</strong> static properties is that each<br />

object or instance created from a class can assign different values to its instance properties,<br />

but all objects of a class share the same values assigned to the class’s static properties.<br />

In Java <strong>and</strong> <strong>Processing</strong>, static variables are used mostly to create constants (with the additional<br />

keyword final added in the declaration). Constants are variables that are<br />

immutable (can’t be changed). For example, in <strong>Processing</strong> <strong>and</strong> Java, PI is a constant, <strong>and</strong><br />

its declaration in Java’s Math package could be something like the following:<br />

static final double PI 3.141592653589793;

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!