CHAPTER 2 ■ JAVA SYNTAX
- The first custom is that each class name should start with a capital letter.
- Second, each logical word within the name should start with a capital letter.
- Third, we rarely use underscores in class names. Dollar signs are used only in
generated code (that is, code written by programs rather than by people).
You can't break the rules (your programs won't work if you do), but you can break the community's
conventions. However, you should have a good reason for doing so. The conventions let us all
communicate more easily with one another, so follow them unless you have a good reason to do
otherwise and comment your reasons.
The fourth word, extends, lets us know the class's parent class. A class's parent (more properly, a
super) class is the class from which the current class gets the bases of its definition. In particular, a class
can use all public and protected methods in its parent class just as though they had been defined in the
class at hand. In this case, we extend Java's root class: Object. The extends keyword tells us that this
class is a variety of its parent class. In this case, AverageImpl is a variety of Object. If you look at the
relationship between a class and its parent class and it doesn't make sense, you need to rethink what
you're doing or, if it's not a class you wrote, more closely examine the library you're using.
The fifth word, Object, tells us the name of the base class that we're extending. Sometimes, the
name is meaningful and tells us something about the parent. Sometimes, as here, the name of the parent
class doesn't tell us much (because every class ultimately extends Object). Good software developers
strive for meaningful names, but it's not always possible.
The sixth word, implements, tells us that this class uses one or more interfaces. We visit them later in
this chapter.
The seventh (and last, in this example) word, Average, tells us the name of the interface this class
implements. A class can implement any number of interfaces, though few implement more than two or
three and many classes implement no interfaces at all.
Fields
A field is a member of a class or (rarely) an interface that holds a value. Fields let us store the bits of
information that we care about when we use a particular class. For example, the height and width fields
in a class that defines rectangles are important to the purpose of the class. In the AverageImpl class listed
earlier in this chapter, the following line defines a field:
private long begin;
This particular field contains a value of type long (which is a really big number—we get to data types
in the next chapter). In this case, the field contains null (a keyword meaning that something has no
value assigned to it and, for object references, refers to no object) until some other bit of code defines it.
You can also directly assign a field's value, as shown in Listing 2-8.
Listing 2-8. Field examples
int[] ints = {1, 2, 3, 4};
AverageImpl averageImpl = new AverageImpl(ints);
For primitives (such as int), you can use a value. In this case, we assign values to an array of ints.
For objects, you have to use the new keyword and call the object's constructor. We cover both the
differences between primitives and objects in the next chapter.