RU beehive logo ITEC dept promo banner
ITEC 120
2009spring
ibarland
nokie
jmdymacek

homeinfolectslabsexamshws
textbooktutor/PIsjava.lang docsjava.util docs

lect04a
declaring fields
and writing javadoc

Reading: §3.2, 3.3, 3.4 .

We will work through the book's BankAccount example: expanding our file of method-stubs by declaring fields, and filling in the stubs. (Here is the stubbed-out version, and the tester class.)

Declaring a field:

class BankAccount {

  double balance;

  // other method (stubs) here…
  }
Declaring a field is just like declaring a local variable, except that it is not inside any method. 1 Correspondingly, when java sees that line, it does not make a labeled box; instead it remembers that in the future, every instance of the class (chest of drawers) will contain a drawer with the given label. So fields are like local-variables that live inside an instance, not inside a method. You can see why the book calls them “object variables”, but in class we'll stick with the term “fields”.

Minor note: Put the adjective “private” in front of every field declaration.

class BankAccount {

  private double balance;

  public BankAccount() {
    }

  // other method (stubs)…
  }
(This means that other classes are not able to access the field.) Conversely, we can put the adjective “public” in front of every method signature (since the whole point of providing methods is so that other classes can call them — just like our RectangleTester called the methods of class Rectangle (but did not directly get/set any of its fields — the methods themselves did that).

We know that with local variables, we should initialize them immediately after declaring them. Unfortunately, this doesn't work with fields (!). Instead, we must initialize them inside the constructor.

class BankAccount {

  private double balance;

  public BankAccount() {
    this.balance = 0.0;
    }

  public BankAccount( double amt ) {
    this.balance = amt;
    }


  // other method (stubs) here…
  }
The purpose of a constructor is to initialize all fields.

To make a deposit, our first version has a local variable:

  public void deposit( double amt ) {
    double updatedBalance = this.balance + amt;
    this.balance = updatedBalance;
    }
Note how we have three types of variables: The local variable updatedBalance (which we declare and initialize), the parameter amt (which we declare in the signature, and Java initializes for us when somebody calls the method), and the field balance (which we declare outside the method, and initialize in the constructor).

We can simplify the above code to get rid of the local variable (although we still need the parameter):

  public void deposit( double amt ) {
    this.balance = this.balance + amt;
    }

Here is the completed code, which now passes all of our same old test cases.


1Also: For local variables, we saw that we could declare them on one line and initialize them on the next, or we could combine those two into a single line. However, for fields, you can't initialize them on the next line: you can either declare them and initialize them immediately, or initialize them inside the constructor.      

homeinfolectslabsexamshws
textbooktutor/PIsjava.lang docsjava.util docs


©2009, Ian Barland, Radford University
Last modified 2009.Feb.11 (Wed)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme