Bruce Eckel's Thinking in Java Contents | Prev | Next

Creating new

data types: class

If everything is an object, what determines how a particular class of object looks and behaves? Put another way, what establishes the type of an object? You might expect there to be a keyword called “type” and that certainly would have made sense. Historically, however, most object-oriented languages have used the keyword class to mean “I’m about to tell you what a new type of object looks like.” The class keyword (which is so common that it will not be emboldened throughout the book) is followed by the name of the new type. For example:

class ATypeName { /* class body goes here */ }

This introduces a new type, so you can now create an object of this type using new:

ATypeName a = new ATypeName();

In ATypeName, the class body consists only of a comment (the stars and slashes and what is inside, which will be discussed later in this chapter) so there is not too much that you can do with it. In fact, you cannot tell it to do much of anything (that is, you cannot send it any interesting messages) until you define some methods for it.

Fields and methods

When you define a class (and all you do in Java is define classes, make objects of those classes and send messages to those objects), you can put two types of elements in your class: data members (sometimes called fields) and member functions (typically called methods). A data member is an object (that you communicate with via its handle) of any type. It can also be one of the primitive types (which isn’t a handle). If it is a handle to an object, you must initialize that handle to connect it to an actual object (using new, as seen earlier) in a special function called a constructor (described fully in Chapter 4). If it is a primitive type you can initialize it directly at the point of definition in the class. (As you’ll see later, handles can also be initialized at the point of definition.)

Each object keeps its own storage for its data members; the data members are not shared among objects. Here is an example of a class with some data members:

class DataOnly {
  int i;
  float f;
  boolean b;
}

This class doesn’t do anything, but you can create an object:

DataOnly d = new DataOnly();

You can assign values to the data members, but you must first know how to refer to a member of an object. This is accomplished by stating the name of the object handle, followed by a period (dot), followed by the name of the member inside the object ( objectHandle.member). For example:

d.i = 47;
d.f = 1.1f;
d.b = false; 

It is also possible that your object might contain other objects that contain data you’d like to modify. For this, you just keep “connecting the dots.” For example:

myPlane.leftTank.capacity = 100;

The DataOnly class cannot do much of anything except hold data, because it has no member functions (methods). To understand how those work, you must first understand arguments and return values , which will be described shortly.

Default values for primitive members

When a primitive data type is a member of a class, it is guaranteed to get a default value if you do not initialize it:

Primitive type

Default

Boolean

false

Char

‘\u0000’ ( null)

byte

(byte)0

short

(short)0

int

0

long

0L

float

0.0f

double

0.0d

Note carefully that the default values are what Java guarantees when the variable is used as a member of a class . This ensures that member variables of primitive types will always be initialized (something C++ doesn’t do), reducing a source of bugs.

However, this guarantee doesn’t apply to “local” variables – those that are not fields of a class. Thus, if within a function definition you have:

int x;

Then x will get some random value (as in C and C++); it will not automatically be initialized to zero. You are responsible for assigning an appropriate value before you use x. If you forget, Java definitely improves on C++: you get a compile-time error telling you the variable might not have been initialized. (Many C++ compilers will warn you about uninitialized variables, but in Java these are errors.)

Contents | Prev | Next