A young fellow approaches me while I’m walking down the street. He tells me to print “You’ll love Java!” so I print those words. If you must know, I print them with chalk on the sidewalk. But where I print the words doesn’t matter. What
•
The name of a class within that package•
An asterisk (indicating all classes within that package) For example, the declarationimport java.util.Scanner;
is valid because java.util is the name of a package in the Java API, and Scanner is the name of a class in the java.util package. The dotted name java.util.Scanner is called the fully qualified name of the Scanner class. A class’s fully qualified name includes the name of the package in which the class is defined. (You can find out all this stuff about java.util and Scanner by reading Java’s API documentation. For tips on reading the documentation, see Chapter 3 and this book’s website.)
Here’s another example. The declaration
import javax.swing.*;
is valid because javax.swing is the name of a package in the Java API, and the asterisk refers to all classes in the javax.swing package. With this import declaration at the top of your Java code, you can use abbreviated names for classes in the javax.swing package — names like JFrame, JButton, JMenuBar, JCheckBox, and many others.
Here’s one more example. A line like
import javax.*; //Bad!!
is not a valid import declaration. The Java API has no package with the one-word name javax. You may think that this line allows you to abbreviate all names beginning with javax (names like javax.swing.JFrame, and javax.sound.midi), but that’s not the way the import declaration works. Because javax isn’t the name of a package, the line import javax.* just angers the Java compiler.
After all this fuss about packages, you may wonder what difference it makes that a class is in one package or another. For some insight, see Chapter 14.
Later that day, an elderly woman sits next to me on a park bench. She says, “An account has a name, an address, and a balance.” And I say, “That’s fine, but what do you want me to do about it?” In response she just stares at me, so I don’t do anything about her account pronouncement. I just sit there, she sits there, and we both do absolutely nothing.
Listing 7-1, shown earlier, is like the elderly woman. This listing defines what it means to be an Account, but the listing doesn’t tell me to do anything with my account, or with anyone else’s account. In order to do something, I need a second piece of code. I need another class — a class that contains a main method. Fortu- nately, while the woman and I sit quietly on the park bench, a young child comes by with Listing 7-2.
LISTING 7-2:
Dealing with Account Objects
package com.example.accounts;
import static java.lang.System.out;
public class UseAccount {
public static void main(String[] args) { Account myAccount;
Account yourAccount;
myAccount = new 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;
out.print(myAccount.name);
out.print(" (");
out.print(myAccount.address);
out.print(") has $");
out.print(myAccount.balance);
out.println();
out.print(yourAccount.name);
out.print(" (");
out.print(yourAccount.address);
out.print(") has $");
out.print(yourAccount.balance);
} }
Taken together, the two classes — Account and UseAccount — form one complete program. The code in Listing 7-2 defines the UseAccount class, and the UseAc- count class has a main method. This main method has variables of its own — yourAccount and myAccount.
In a way, the first two lines inside the main method of Listing 7-2 are mislead- ing. Some people read Account yourAccount as if it’s supposed to mean that
“yourAccount is an Account” or that “the variable yourAccount refers to an instance of the Account class.” That’s not really what this first line means.
Instead, the line Account yourAccount means, “If and when I make the variable yourAccount refer to something, that something will be an instance of the Account class.” So, what’s the difference?
The difference is that simply declaring Account yourAccount doesn’t make the yourAccount variable refer to an object. All the declaration does is reserve the variable name yourAccount so that the name can eventually refer to an instance of the Account class. The creation of an actual object doesn’t come until later in the code, when Java executes new Account().
Technically, when Java executes new Account(), you’re creating an object by call- ing the Account class’s constructor. When you see Java’s new keyword, think
“constructor call.” I have a lot more to say about constructors and constructor calls in Chapter 9.
When Java executes the assignment yourAccount = new Account(), Java creates a new object (a new instance of the Account class) and makes the variable yourAc- count refer to that new object. (The equal sign makes the variable refer to the new object.) Figure 7-2 illustrates the situation.
To test the claim that I made in the last few paragraphs, I added an extra line to the code of Listing 7-2. I tried to print yourAccount.name after declaring yourAc- count but before calling new Account():
Account myAccount;
Account yourAccount;
out.println(yourAccount.name);
When I tried to compile the new code, I got this error message: variable yourAc- count might not have been initialized. That settles it. Before you do new Account(), you can’t print the name variable of an object because an object doesn’t exist.
When a variable has a reference type, simply declaring the variable isn’t enough.
You don’t get an object until you call a constructor and use the keyword new.
For information about reference types, see Chapter 4.