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

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • The proliferation of cloud computing options has begun to change the way storage is thought about, procured, and used. IT managers and departments need to think through how cloud options might fit into and complement their onsite data infrastructures. This white paper explains cloud storage and backup, providing advice about the tools and best practices for its implementation and use. Read this white paper for some useful takeaways about how to take advantage of cloud storage for high availability, backup and …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds