RU beehive logo ITEC dept promo banner
ITEC 120
2008fall
aaray,
ejderrick,
ibarland,
jmdymacek

homeinfolabshwsexams
textbookjava.lang docsjava.util docsarchive

lect03b
signatures; stub methods

Signatures are covered in §3.1, 3.2. The mantra for a signature is:

typereturn name( parameter-type-and-name )

Stub functions

The problem with just writing, say,

public double getBalance() {
  ...
  }
is that the ... doesn't actually compile. If you just want to check that your program (otherwise) compiles, you can replace the book's ... with something which is syntatically correct (but is assuredly a logic-error). Since getBalance is contracted to return a double, we'll just return any ol' double:
  public double getBalance() {
    return -12.345;
    }
This satisfies the signature, so the program will compile. (Of course, it's wrong that every BankAccount has a balance of negative $12.345. That's fine, we'll worry about the correct code later; for now it's important that we double-check we don't have any silly syntax errors before we go any further.) This is called a stub method, because we have placed a stub return in place of the real return we'll use eventually.

Of course, if a method's signature says it returns a String, then our stub function must comply:

  public String getOwnerName() {
    return "Otto von Schnitzelpusskrankengescheitmeyer";
    }

What about void methods, that don't return a value at all? You can leave those empty:

  public void deposit() {
    }

  // This compiles as-is.

(Constructors are like void methods, in this respect: they don't explicitly return a value, even though conceptually they are returning a whole new object.)

Another example

Problem 1: Our boss wants us to build GPSUnits for people to put in their car.

Solution 1: We can already write code for this:

public class GPSUnit {

   

  }
In a moment, we'll fill this in, but it's valid code as-is, and it compiles right now! (I always press compile even after typing in only this much.)

Problem 2: Our boss tells us what sort of things our GPSUnit can do: it can

homeinfolabshwsexams
textbookjava.lang docsjava.util docsarchive


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