Next: Using Account. Up: Defining an object. Previous: None.


The Account class

Textbook: Section 3.3

Ok, so how do we define our own type?

Let's do an example: We're going to create a type called Account to represent a bank account. (Think of this object definition as being part of a much larger banking system.)

To design our object, we ask ourselves two questions.

What data is associated with an object?

In the case of a bank account, one important piece of data is the balance - the amount of money currently in the account. You can imagine other pieces of data too (like maybe the account's owner), but actually in our example for the moment we'll just have the balance.

To define an Account type containing this piece of data, we would write the following into a file called Account.java.

import csbsju.cs160.*;

public class Account {
    private double balance;

    public Account() {
        balance = 0.0;
    }
}
Notice that we declared the balance field to be a double type, and to be private. We declared it private because we don't want other people to go around messing with balance without using one of our officially sanctioned methods that we're about to define here in a minute.

See how we omitted the static keyword when we declared the instance variable? We'll see this when we get to methods also: the static keyword indicates that the member is a class member instead of a instance member (the default, if you don't say static). So you'd use the static keyword if the variable were a class variable - but you'll omit it if you want an instance variable.

The second part - which looks mysteriously like a method definition - is called the constructor method. It's purpose is to initialize the fields of Account to appropriate first values. In this case, it sets the balance value to start at 0.0.

What actions can we perform on the object?

So what can we do to a bank account?

  1. We can query it to get the current balance.
  2. We can deposit money into it.
  3. We can withdraw money from it.
And so we'll define three methods as part of our class definition, specifying how to perform each of these actions.

import csbsju.cs160.*;

public class Account {
    private double balance;

    public Account() {
        balance = 0.0;
    }

    public double getBalance() {
        return balance;
    }

    public void deposit(double amount) {
        balance += amount;
    }

    public void withdraw(double amount) {
        balance -= amount;
    }
}

These are quite similar to the methods we defined earlier, except that you'll notice that they don't contain the word static. These are instance methods, whereas the static methods we've used heretofore were class methods. Instance methods apply only to particular objects (instances) of type Account (which have associated with them a balance field). The static methods were associated with the class, not with particular objects of that class.

You'll notice that we've defined the methods to be public. That's because we want to allow other people to employ these methods in order to manipulate an Account object.


Next: Using Account. Up: Defining an object. Previous: None.