class BankAccount { int number; double balance; String name; }That's it for now. We've just defined our first class. A good way to think about a class is that it is a cookie cutter, template, or pattern for manufacturing objects of a certain kind.
Remember, it's important to distinguish between classes and objects. When we analyze the world around us, we can identify concrete things, such as a desk, a dog, or a car. These things are concrete -- that is, they have properties that we can directly sense. However, there are less concrete things that we can conceive of as well, such as the concept of car. It's important to distinguish the concept of a car as being in some way different from a particular instance of a car (such as the car in your driveway). The concept of car seems to exist only in our mind, while an actual car exists as a physical object in the real world. Aristotle called the concept a universal and the actual physical thing a particular. Roughly speaking, this distinction between a universal and a particular is just the difference between class and object in Java. A class is a description of the qualities, behavior and implementation that a given set of objects have in common. Objects are instances of classes. They have concrete representations and behavior.
Here is the most basic pattern for a class definition:
class <class-name> { <instance variable declarations> }In our example, we've defined a class called
BankAccount
.
Each instance of that class will have three variables (names)
associated with it: number, balance, and name. Notice the types that
we have chosen for our instance variables. The balance is a
double
, the account number is an int
, and
the name is a String
. These types were not chosen by
accident, but because they seem reasonable. This is not to say that
the types we have chosen are necessarily the best or the only types
that we could have chosen. We'll discuss possible variations later,
but for now we'll stick with these.
Just as my real-world bank account will have a different balance than yours (withdrawals from my account change my balance, not yours), two BankAccount objects we create in Java will have different balances. Let's create a BankAccount object:
BankAccount account = new BankAccount();What are the values of its pieces? Java guarantees that all names are bound to well-defined zero values by default, but usually these values aren't interesting, and it's arguably poor style to depend on them, so let's create an account and set the values of its pieces:
BankAccount account = new BankAccount(); account.balance = 100.25; account.number = 1234; account.name = "Bob";Notice that when we created a new BankAccount, and named it
account
, it is as if three more names were introduced:
balance, number, and name. These names belong to the new account, so
to use them, we have to use the "dot" syntax (".") to access them.
The dot syntax allows us to get access to names that belong to a
given object. Those names may be method names (as we've seen already)
or variable names (as we're seeing here).
Notice also that these statements assign new values to the names of the account -- they change the state of the bank account. In a way, we are rebinding the names to new values. In general, the pattern for an assignment looks like this:
<name> = <expression>First the expression on the right hand side of the '=' is evaluated, and then the name is bound to that value.
Let's draw a picture of the account at this point:
How would we deposit 15 dollars into our account? We can do so as follows:
double oldBalance = account.balance; account.balance = oldBalance + 15.0;This works, although it is more convenient to express it as follows:
account.balance = account.balance + 15.0;Remember, in an assignment, the expression on the right hand side of the '=' is first evaluated, then bound to the name on the left hand side.