Classes are arguably the most important aspect of Java — or of any object-oriented language.

The relationship between classes and objects is a close one, so we’ll examine that relationship here.

What is a Class?

Humans classify things all the time.

It helps us from drowning in details:

“What vehicle ran over you, madam?”
“A blue Buick four-door sedan.”

rather than:

“What vehicle ran over you, madam?”
“Why, officer, it had a blue body, four wheels, four doors, six windows, a front, back and two sides of some kind of metal. There were four people sitting inside it, and one of them was driving. Each door had a window, a handle, and pinstripes, was about five feet high and four feet wide. In front, there was a shiny metal grille, and …”

For example:

  • Dog is a distillation of the common attributes and behaviors of dogs of all sizes, types, and ferocities.
  • Car is a similar distillation applied to cars of all sizes, colors, speeds, styles, makes, etc.
  • Nut describes common attributes of Filberts, Brazils, Almonds, Macadamias, etc. Or it describes the attributes of the kind of Nut that attaches to a Bolt (or perhaps even yours  truly…)
  • Road describes the common attributes of dirt tracks, town streets, county roads and superhighways.
  • Student encompasses those of types young and old, single and married, male and female, tall and short, undergraduate, graduate, etc.
  • College describes Technical Colleges, Liberal Arts Colleges, Colleges of Art and Design, Universities, etc.

To perform this kind of classification, we need to distill out:

  • Attributes or Properties (has fur, four legs, a tail, sharp teeth, etc.)
  • Behaviors or Operations (barks, walks, runs, wags tail, etc.)

Here’s an example of a Java class:

public class Dog
{
    public void StartBarking() { /* ... */ }
    public void StopBarking() { /* ... */ }

    public int getAge()   { return m_age; }
    public int getColor() { return m_color; }
    public int getTemperament() { return m_temperament; }

    public static final int DOCILE = 1;
    public static final int GOOD_WITH_CHILDREN = 2;
    public static final int VICIOUS = 3;

    int m_age;
    int m_color;
    int m_temperament;
}

Here’s another example:

import java.lang.Math;

public class Circle
{
    // Accessor methods
    public double getX()      { return m_x; }
    public double getY()      { return m_y; }
    public double getRadius() { return m_r; }
    // Mutator methods
    public void setX(double v)      { m_x = v; }
    public void setY(double v)      { m_y = v; }
    public void setRadius(double r) { m_r = r; }
    // Operations/Attributes
    public double getCircumference()
                { return 2 * Math.PI * m_r; }
    public double getArea()
                { return Math.PI * m_r*m_r; }

    double m_x, m_y;	// coordinates of center
    double m_r;		// radius
}

Note: Think of a class as a template, or a recipe from which to make instances of that class.

Note: It’s hard to tell an abstract concept “Dog” to go fetch!

What is a Class Instance?

class is a template from which to make an object

An instance of a class is an object.  (This is somewhat loose terminology, but the two terms are usually considered synonyms.)

  • Fido is an instance of class Dog
  • FredBloggs is an instance of class Person

For example, given a class like this:

class BankAccount
{
   // Instance methods
   double deposit(double amount)
   {
      m_balance += amount;
      return m_balance;
   }

   double withdraw(double amount)
   {
      m_balance -= amount;
      return m_balance;
   }

   String getHolder()
   {
      return m_holder;
   }

   // Instance variables
   String m_holder;
   double m_balance;
}

we can instantiate an instance of this class as follows:

BankAccount ac = new BankAccount();

The actual instantiation is performed in:

new BankAccount();

while:

BankAccount ac;

is just a reference to the instance.

The instance is created in two stages:

  1. The new operator creates the space for the class instance
  2. constructor is called to initialize the instance

Note: More about constructor methods very soon…