Creating new data types: class | CodeGuru

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Mar 1, 2001
3 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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
CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.