As stated at the beginning of this chapter, classes are the building block in an object-oriented language such as Java. In fact, Java, unlike C++, goes so far as to make it impossible to define any variables or methods outside a class. Everything you do in Java is based on designing and implementing classes.
Before you start creating programs, you must first learn how to create classes. In terms of syntax, there are two parts to a class in Java: the declaration and the body. Listing 7.1 is a simple class that fulfills some of the requirements of the simple bank account example discussed in the previous section. Examine this listing to get an idea of what constitutes a class. You can refer to this listing again later as your understanding of classes grows. Listing 7.1 GeneralAccount.java ” A General Class for Maintaining a Bank Account/** * A simple class used to implement common behavior for various * types of bank accounts. It should be extended to implement * behavior for specific accounts. */ public class GeneralAccount { // unique account identifier private String accountNumber; // this will hold the current account balance protected float balance; public GeneralAccount( String accountNum ) { accountNumber = accountNum; } public String getAccountNumber() { return accountNumber; } public float getBalance() { return balance; } public void makeDeposit( float amount ) { balance += amount; } public void makeWithdrawal( float amount ) { balance -= amount; } } Take a quick look through this class. The first part of any class is the class declaration. Most class declarations you write will look similar to that for GeneralAccount: public class GeneralAccount Declaring a class states several things, but probably the most important one is the name of the class ( GeneralAccount ). In the case of any public class, the name of the class must also match the name of the file that contains it. In other words, this class must appear in the file GeneralAccount.java. Caution Remember that Java is a case-sensitive language. This case sensitivity applies to filenames also. In the preceding example, naming the source file that contains the GeneralAccount class generalAccount.java or anything else other than GeneralAccount.java results in a compiler error. The next part of the class is the opening brace. You should notice that there is a brace ( { ) at the beginning of the class, and if you look all the way down at the bottom there, is also a closing brace ( } ). The braces define the area in the file where the class definition will exist. Also, note that there is no semicolon at the end of the final closing brace like you find in C++. A bit farther down, you will see several comments. As you learned in "Comments" (Chapter 3, "Data Types and Other Tokens" ), comments can exist anywhere in the file and are ignored by the compiler, but they help you leave messages for yourself or other programmers. Next, you will see several fields declared. Each of these variables is accessible from any of the methods in the class. When you change them in one method, all the other methods will see the new value. |