Explaining classes can be lengthly, I send you to the
http://processing.org/learning/objects tutorial.
But explaining the difference between functions and methods needs that you understand classes...
Oh well, just covering the bases should lighten some things.
You don't use a class "rather than a function". You use a class when you need an object. Which is the official word for class instance. Think of classes like a blueprint: you define variables (named 'fields' here) and functions (named 'methods' in this context). Then you instance the classes to objects, you make several of them using the class definition as a mold.
Simple (simplistic) example:
- class Person
- {
- // Fields: the data associated to the object
- String name;
- int age;
-
- // Constructor: you create objects by calling this special function allowing to initialize the data
- Person(String n, int a)
- {
- name = n;
- age = a;
- }
-
- // Methods: these are simple functions, but acting only on the object from the class they belong to
- void makeOlder()
- {
- age++;
- }
-
- // Polymorphism: same name, different parameters
- void makeOlder(int n)
- {
- age += n;
- }
- }
Then you create objects like:
Person employee = new Person("John", 32);
and you invoque methods like:
employee.makeOlder();
Somehow, functions are a concept from other languages like C or Basic, they don't really exist in Java, but Processing re-created them by using a simple trick (beyond this discussion), so you can just call
fill(200) instead of
g.fill(200) for example. Ie. they are called without an (apparent) object.