Instead, you want to know how my account is different from your account. After all, my account is named Barry Burd, trading as Burd Brain Consulting, and your account is named Jane Q. Reader, trading as Budding Java Expert. My account has $24.02 in it. How about yours?
When you come right down to it, the differences between one account and another can be summarized as values of variables. Maybe there’s a variable named balance
. For me, the value of balance
is 24.02
. For you, the value of balance
is 55.63
. The question is, when writing a computer program to deal with accounts, how do I separate my balance
variable from your balance
variable?
The answer is to create two separate objects. Let one balance
variable live inside one of the objects and let the other balance
variable live inside the other object. While you’re at it, put a name
variable and an address
variable in each of the objects. And there you have it: two objects, and each object represents an account
. More precisely, each object is an instance of the Account class.
So far, so good. However, you still haven’t solved the original problem. In your computer program, how do you refer to my balance
variable, as opposed to your balance
variable? Well, you have two objects sitting around, so maybe you have variables to refer to these two objects. Create one variable named myAccount and another variable named yourAccount. The myAccount
variable refers to my object (my instance of the Account
class) with all the stuff that’s inside it. To refer to my balance, write
myAccount.balance
To refer to my name, write
myAccount.name
Then yourAccount.balance
refers to the value in your object’s balance
variable, and yourAccount.name
refers to the value of your object’s name
variable. To tell Java how much I have in my account, you can write
myAccount.balance = 24.02;
To display your name on the screen, you can write
out.println(yourAccount.name);
What it means to be an account.
public class Account {
String name;
String address;
double balance;
}
The Account
class defines what it means to be an Account
. In particular, this code tells you that each of the Account
class’s instances has three variables: name
, address
, and balance
. This is consistent with the information in the image above.1. Java programmers have a special name for variables of this kind (variables that belong to instances of classes). Each of these variables — name
, address
, and balance
— is called a field.
A variable declared inside a class but not inside any particular method is a field. The variables name
, address
, and balance
are fields. Another name for a field is an instance variable.
Account
class, those existing things are two String
values and a double
value.
The field declarations have default access, which means that a word wasn’t added before the type name String
. The alternatives to default access are public, protected, and private access:
public String name;
protected String address;
private double balance;
Professional programmers shun the use of default access because default access doesn't shield a field from accidental misuse. But, you learn best when you learn about the simplest stuff first, and in Java, default access is the simplest stuff.