|
Summary: Simon continues his exploration of the Java language... with sample code. Into Java, Part II - Lab WorkBy: Simon Gronlund Welcome to the hands-on part of this Java column. Up to now we know of classes, methods, constructors and data fields, and that really seems to be a lot, isn't it? In spite of that, we cannot rush of and use the more subtle parts of Java this time, we will control ourselves and stick to text mode. Nevertheless, we will make ourselves one class and a simple test application to bring our class alive. A few more details will be discussed, like header, body, toString, new and a few syntax details. Yes, I am well aware of this being a veeeeery long column. Isn't it always so, at first there are quite a few things to get the hang of, and later it doesn't feel much. A few years back I started out learning the Persian language, a spoken language, not a programming language. In the beginning the curve was steep, believe me. Not a single character was alike our western alphabet, but as soon as I got hold of it, everything seemed much easier. So this Java column will, too. Again I'd like to repeat that a basic programming knowledge is assumed. Most of the code is understood by the context, but a good source of general programming knowledge is the REXX column by Dr. Dirk Terell, began in OS/2 e-Zine! v3n4. The general coding style is coherent within this family of programming languages. The Bank AccountIn any object oriented language, the Bank Account (BA) is one of the most commonly used illustrations to discuss the basics of classes and objects, and we will use it as well. A BA is an object, in this case a record of our means. Creating a BA as a class is wise. Whenever the senior accountant finds out that we have to change a few things in the BA's, it's an easy task. Do it once in the classes affected, and we're almost done. In languages not object oriented, we have to search most of the code to find out if we need to adjust anything.The first thing we always will do is to make a rough outline of what we expect of a new class. This technique is always useful, my first computing lecturer pointed out that "application code is 90 percent penciled and the remainder is typed by the keyboard". I start to believe it's true, most of the 90 percent anyway. If we skip this part we soon will have to pay with blood, sweat and tears. Any BA uses a name for the account holder, a balance, a way to deposit and redraw money. For a lite BA that's all, the first pencil part is done. Let's make ourselves a new folder within our OS/2-eZine_Java folder, named Bank. Now we will continue with writing ourselves a class named BankAccount using our favorite editor, and we will save it into this new folder using the name BankAccount.java.
Line 1: A statement that informs the Java compiler to import
from the Line 2: The class header, providing the class
name, At the end of line two there is the { , the beginning of the body of the class. Along with the finishing } it encompasses the entire class body. This far the body is nothing more than two data fields, the name and the balance. Line 3: We assume Line 4: Preferable we will only handle real dollars and never
cents, that's why we declare This initialization to zero is not necessary to do here, but it's convenient.
Even though the bank will use welcome gifts in the future, or only for certain BA's,
it doesn't hurt to initiate to zero. On the contrary, think of what could happen
if we didn't initiate to zero, and we'll forget to give ProtectedWhat about that protected at the beginning of line three and four?
We said that the class is made public , so anyone could use it to make
their own BA's from. But we will certainly not let anyone else touch the stuff we
have within our BA object. Therefore, we hide the information
within the class, we protect it so that no-one could not "see" or manipulate
the data fields. Now only methods within the class could change or access
the data. There are other access modifiers as well, that we'll discuss when we encounter
them.
MethodsNow we have to make ourselves several methods, a few already mentioned, which we could start with. Any method is put within the body of the class. In the same manner as a class, methods too have headers, and bodies encompassed by { and }. Let's start.The very first method, a method we will always use, is the
Line 6 and 9: As the class is, methods are made What about the Please note, whenever we mention a method, usually we will write Line 10 and 12: Here we do a simple An Please note the { } encompassing the body of the Line 11: Add the sum of money to the balance. Within the object the data fields are visible. They are only invisible to objects and methods outside the object they belong to, because we made them protected. Line 13: A line with this text will be printed if the cashier made that typo, and nothing will happen to your balance. Try to deposit again. Line 15: The wrong value will be printed. Compared to the HelloWorld,
we see that it would have been possible to use the What is a BA if we cannot withdraw money from it? Let's continue:
Line 18: Despite we withdraw money, it isn't the Line 19: There is no big difference from the In the middle of the comparison line there is a double Why did we write the "do we have enough money" check before
the "typo" check? Despite all those bugs we see too often, developers
actually have been thinking. One thing with an AND comparison is really
trivial: both left side and right side have to be true. If the Exactly the same reasoning is true about OR comparisons, it's enough finding one true left OR true right. Put the most frequently true to the left. Every composite comparison will evaluate from left to right, and stop whenever a conclusion is made.
These two useful methods will give us an opportunity, the only one indeed, to have a look at the two data fields. Since the fields are protected it isn't possible for anything, except public querying methods within the object, to return those data. Line 27 and 30: The double parentheses ( ) are empty, nothing is needed to be able to return the data stored in the data fields. To show the difference, we will make a method used whenever we would like to change a name. Maybe the customer got married, or whatever.
How come both methods could use the same name Now we have a complete class, with two data fields and six methods. Everything is written within the encompassing { } parentheses of the class. ConstructorCould we now make an instance out of it? Actually not! The constructor will come to play an important role. At first, we could always make the basic constructor, which takes no parameters at all. That's the easiest one, but not always to recommend. Think of a database record, why create a new record but without data stored in it. Mostly we press Cancel those times, don't we? Other times it suits our intention very well to make zombie objects, and later bring them alive.
Constructors looks a lot as methods, and they are in many respects. But we cannot
invoke them, as we invoke methods. And they never returns anything. Constructors
will only listen to a special operator, the What is Anything else isn't done within this constructor. The object is now created,
we have our BankAccount instance at hand. However, if we didn't initiate
To make this class work, we have to use the code found in MyBank.java. Objects like BA are rarely running by themselves. MyBank is a short test application. It will create a few BA objects, assign them owners and do a few transactions.
new operator. Above I stated that
the constructors only listened to a special operator, that's new . This
operator will order the creation of a new instance of the actual class. It's as
simple as that. Next time we will discuss it a little bit more.
After you have edited MyBank.java, copy it into the Bank folder. As we did the last month, we compile this Java file into Java byte code, and then run it. Why don't we compile the class BankAccount.java? Simply because Please, experiment with MyBank as much as you like, nothing will be crashed or screwed up, the nasty and foul-mouthed compiler will amuse you. Ctrl + C will stop every process out of control. SummaryWe have labored hard this time, examining the difference between class and object, the blueprint vs. the real object, created by the new operator invoking the constructor. Both classes and methods are made by headers and bodies, the headers providing vital information to us and the compiler.The data fields have to be declared and maybe initialized. Initiation could be made at the declaration, or in the constructor. Neither is absolutely necessary, we might give data field's values later on. The importance of the penciling phase and of toString() was emphasized, since it will save us a lot of late nights and brain work in the future. With this knowledge we now are prepared to advance into the Java world of living creatures, objects and ways to master them all. Have a nice adventure. See you next month!
|
Copyright © 1999 - Falcon Networking | ISSN 1203-5696 | November 1, 1999 |