Creating new data types: class

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

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;



Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds