How to Define a Java Method within a Class (Displaying an Account)

Java lets you define a method within a class. Imagine a table containing the information about two accounts. (If you have trouble imagining such a thing, just look at the table below.)

Without Object-Oriented Programming

Name

Address

Balance

Barry Burd

222 Cyberspace Lane

24.02

Jane Q. Public

111 Consumer Street

55.63

In this table, each account has three things — a name, an address, and a balance. That’s how things were done before object-oriented programming came along. But object-oriented programming involved a big shift in thinking. With object-oriented programming, each account can have a name, an address, a balance, and a way of being displayed.

In object-oriented programming, each object has its own built-in functionality. An account knows how to display itself. A string can tell you whether it has the same characters inside it as another string has. A PrintStream instance, such as System.out, knows how to do println. In object-oriented programming, each object has its own methods. These methods are little subprograms that you can call to have an object do things to (or for) itself.

And why is this a good idea? It’s good because you’re making pieces of data take responsibility for themselves. With object-oriented programming, all the functionality that’s associated with an account is collected inside the code for the Account class. Everything you have to know about a string is located in the file String.java. Anything having to do with year numbers (whether they have two or four digits, for instance) is handled right inside the Year class. Therefore, if anybody has problems with your Account class or your Year class, he or she knows just where to look for all the code. That’s great!

Imagine an enhanced account table. In this new table, each object has built-in functionality. Each account knows how to display itself on the screen. Each row of the table has its own copy of a display method. Of course, you don’t need much imagination to picture this table. Check out this table.

The Object-Oriented Way

Name

Address

Balance

Display

Barry Burd

222 Cyberspace Lane

24.02

out.print …

Jane Q. Public

111 Consumer Street

55.63

out.print …

An account that displays itself

In the second table, each account object has four things — a name, an address, a balance, and a way of displaying itself on the screen. After you make the jump to object-oriented thinking, you’ll never turn back. The code below shows programs that implement the ideas in the second table above.

In this code, an account displays itself

import static java.lang.System.out;

public class Account {

String name;

String address;

double balance;

public void display() {

out.print(name);

out.print(" (");

out.print(address);

out.print(") has $");

out.print(balance);

}

}

This code uses the improved account class.

public class UseAccount {

public static void main(String args[]) {

Account myAccount = new Account();

Account yourAccount = new Account();

myAccount.name = "Barry Burd";

myAccount.address = "222 Cyberspace Lane";

myAccount.balance = 24.02;

yourAccount.name = "Jane Q. Public";

yourAccount.address = "111 Consumer Street";

yourAccount.balance = 55.63;

myAccount.display();

System.out.println();

yourAccount.display();

}

}

In the first set of code, the Account class has four things in it: a name, an address, a balance, and a display method. These things match up with the four columns in the second table. So each instance of the Account class has a name, an address, a balance, and a way of displaying itself. The way you call these things is nice and uniform. To refer to the name stored in myAccount, you write

myAccount.name

To get myAccount to display itself on the screen, you write

myAccount.display()

The only difference is the parentheses.

When you call a method, you put parentheses after the method’s name.

The display method’s header

Look again at the code above. A call to the display method is inside the UseAccount class’s main method, but the declaration of the display method is up in the Account class. The declaration has a header and a body. The header has three words and some parentheses:

The word public serves roughly the same purpose as the word public in the first set of code. Roughly speaking, any code can contain a call to a public method, even if the calling code and the public method belong to two different classes. In the example above, the decision to make the display method public is a matter of taste. Normally, when you create a method that’s useful in a wide variety of applications, you declare the method to be public.

The word void tells Java that when the display method is called, the display method doesn’t return anything to the place that called it.

The word display is the method’s name. Every method must have a name. Otherwise, you don’t have a way to call the method.

The parentheses contain all the things you’re going to pass to the method when you call it. When you call a method, you can pass information to that method on the fly. The display method in the first set of code looks strange because the parentheses in the method’s header have nothing inside them. This nothingness indicates that no information is passed to the display method when you call it.

For Dummies: The Podcast

Check out the brand new podcast series that makes learning easy with host Eric Martsolf.