Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Sams Teach Yourself Visual J++ 6 in 21 Days
(Publisher: Macmillan Computer Publishing)
Author(s): Rick Leinecker
ISBN: 0672313510
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Declaring Constructors

When you declare a class in Java, you can declare constructors for the class. Constructors are methods that perform the required initializations for each new instance of a class. Java constructor methods have the same name as the class to which they belong. They don’t return anything. They can take zero or more parameters, and they can be overloaded. Pretty cool, huh?

Constructors are used to ensure that all new objects are created in a valid initial state. Because Java objects are always created from the heap, object constructors are called only in conjunction with a new keyword. In fact, it is illegal to call a constructor at any time other than when creating an object with a new keyword.

For example, the following code shows a legal constructor call, and an illegal constructor call:

SomeOtherItem Item;

Item = new SomeOtherItem(); // SomeOtherItem()
[ccc]constructor is called here legally.

Item.SomeOtherItem(); // SomeOtherItem()
[ccc]constructor is called illegally here.

All Java classes have constructors. If you do not declare a constructor, the compiler automatically creates a default constructor for you. Java guarantees that all data members are initialized as we said before, and that all uninitialized data members are assigned default values.

Constructors can call other constructors in the same class, or constructors in its superclass. Calls to other constructors must be the first statement in any constructor. If no other constructor of the superclass is invoked, the default constructor of the superclass is called automatically. We’ll talk a little bit more about the super() method later.

Creating and Destroying Objects

You can create an object in Java by placing the new keyword in front of a call to the object’s class constructor. This operation allocates memory for the object, calls the constructor that follows the new keyword, and returns a reference to the newly created and initialized object. The Java environment keeps track of all memory allocations and automatically frees memory when a resource is no longer needed. You never need to delete an object’s memory as you do in other languages.

Comparing Objects

Java provides two operators for comparing objects: the equality (==) and the inequality (!=). These operators test whether two object references refer to the same object. Because Java does not have an operator overloading function, you can’t change the behavior of the equality operators. If you want to compare the contents of two objects, you have to write a method of your own to perform this function. The Java Object class, from which most of your classes will be extended, provides an equals method you can override. The Object class’s equals() method compares two object references, like the equality operator.

Copying Objects

The Java Object class provides a clone() method that can be used to create copies of objects. The default implementation of this method creates a clone by performing a bitwise copy of the original object. This bitwise copy might or might not be the appropriate thing to do for any given class.

A bitwise copy of an object is allocated and then copied bit by bit. Although a completely new object is created, the original object is copied, including its references to other objects.

Cloning objects that contain only pure data types is simple. You must be careful, however, when cloning objects that reference other objects. The default clone() method does not make copies of the referenced objects. The cloned object references the same objects that are referenced by the original object.

Using Inheritance

Java supports inheritance, the capability of a class to inherit attributes and behaviors from its parent, or superclass. The extends keyword is used to establish an inheritance relationship between a subclass and a superclass. We’re going to start this section with a simple example. I’ve created an entire applet. Within this applet is the declaration for a class called Shape. The Shape class has three methods. The first method, the draw() method, simply draws just the shape. The second method, the setPosition() method, takes two integer arguments: the new x and y positions for the shape. The third method, the movePosition() method, takes two arguments: the distance to move the x coordinate, and the distance to move the y coordinate. Listing 5.2 shows this applet, and Figure 5.2 shows this applet running.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.