05 – Static vs. Instance

As simple as classes are there is much we still need to learn about them, so the next several sections are divided into more detailed topics about classes. The first and possibly the most challenging to understand is what is meant by static attributes versus instance attributes.

So far in our sample code our methods have all been statically defined. That makes them available to any class without creating an instance of the class they are defined in. Creating an instance of a class is the most common way that a class is accessed and it allows you to store data specific to the instance of the class along with whatever methods are available to that class. That may seem a bit circular so it may be better expressed with an analogy.

For example, if we wanted to create an abstract representation of a car in our program we would write a car class the encapsulated the critical features of a car in Java code. That class would be a representation of a car idea, though, and not a representation of a specific car. If we wanted a specific car, we would create an instance of that class and programmatically set the attributes of the car to meet our needs.

To make the example more concrete let’s look at an example car class that has a mixture of static and instance methods in it:


public class Car {
  private int speed;
  private String color;
  // This method is our constructor
  // we use it to set some initial conditions.
  public Car() {
    speed = 0;
    color = “unknown”;
  }
  // Set our car's speed
  public void setSpeed(int value) {
    speed = value;
  }
  // Get our car's speed
  public int getSpeed() {
    return speed;
  }
  // Set our car's color
  public void setColor(String value) {
    color = value;
  }
  // Get our car's color
  public String getColor() {
    return color;
  }
  // Determine if an object happens to be a car
  public static boolean isCar(Object thing) {
    if (thing instanceof Car) {
      return true;
    } else {
      return false;
    }
  }
}

This class has a number of interesting features that hopefully help illustrate some of the differences between class and instance methods and variables.

First note that the variables are instance variables (they are not static) and they are private. That means that they can only be accessed by this class and not any external class (this is good programming practice – allowing other code to alter our variables can cause unexpected results). Second, because of this limitation we create what are called accessor methods to get and set these variables. This is a common practice in Java programming as mentioned above. Forcing other code to alter our state through methods we provide in our code means that we as programmers can be in control of the state of our classes.

Lastly this class contains one class method (or static method) that simply helps determine if an object (an instance of a class) is a Car object or not. Let’s take a look at how this class and it’s features might be used within a program.


// Skipping some imports and other stuff
// we'll talk about later.
public class TestCar1 {
  public static void main(String[] args) {
    // Create an instance of Car called myCar
    Car myCar = new Car();
    // Call myCar's instance methods
    myCar.setSpeed(55);
    myCar.setColor(“red”);
    // Going to call the isCar test method
    // statically from the Car class
    boolean test = Car.isCar(myCar); // true!
    String s = “I'm not a car!”;
    test = Car.isCar(s); // false!
  }
}

Hopefully at last the difference between an instance of a class and the class itself is clear now. In the first line of our program above we create a car object – an instance of car. We then proceed to call two of the methods of our car instance. Finally, we perform two tests (although we never do anything with the answer) using the static isCar method from the Car class.

Static vs. Instance Exercise

Create a class using the following stub, which describes a human in code. It should contain an instance variable (not static) to store the person’s age, methods to get and set the age, and a static method to return a String description of a person (in general, not your particular person created when you instantiate your object).


public class Human {
  // Instance Variables
  // Instance Methods
  // Class Method
}