Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
TheObject
class sits at the top of the class hierarchy tree. Every class is a descendant, direct or indirect, of theObject
class. This class defines the basic state and behavior that all objects must have, such as the ability to compare oneself to another object, to convert to a string, to wait on a condition variable, to notify other objects that a condition variable has changed, and to return the class of the object.The following is a list of handy methods that the
Object
class provides:With the exception of
clone
equals
andhashCode
finalize
toString
getClass
notify
,notifyAll
, andwait
notify
,notifyAll
, andwait
, these methods are covered in the sections that follow. Thenotify
,notifyAll
, andwait
methods all play a part in synchronizing the activities of independently running threads in a program, which is discussed in Threads: Doing Two or More Tasks At Once.
You use theclone
method to create an object from an existing object. To create a clone, you write:aCloneableObject.clone();Object
's implementation of this method checks to see whether the object on whichclone
was invoked implements theCloneable
interface. If the object does not, the method throws aCloneNotSupportedException
. Even thoughObject
implements theclone
method, theObject
class is not declared to implement theCloneable
interface, so classes that don't explicitly implement the interface are not cloneable. If the object on whichclone
was invoked does implement theCloneable
interface,Object
's implementation of theclone
method creates an object of the same type as the original object and initializes the new object's member variables to have the same values as the original object's corresponding member variables.The simplest way to make your class cloneable, then, is to add
implements
Cloneable
to your class's declaration. For some classes, the default behavior ofObject
'sclone
method works just fine. Other classes need to overrideclone
to get correct behavior.Consider a
Stack
class that contains a member variable referring to an array ofObject
s. IfStack
relies onObject
's implementation ofclone
, the original stack and its clone refer to the same array. Changing one stack changes the other, which is undesirable behavior.Here is an appropriate implementation of
clone
for ourStack
class, which clones the array to ensure that the original stack and its clone do not refer to the same list:The implementation forpublic class Stack implements Cloneable { private Object[] items; private int top; ... // code for Stack's methods and constructor not shown protected Stack clone() { try { Stack s = (Stack)super.clone(); //clone the stack s.items = (Object)items.clone(); //clone the array return s; // return the clone } catch (CloneNotSupportedException e) { //This shouldn't happen because Stack is Cloneable. throw new InternalError(); } } }Stack
'sclone
method is relatively simple. First, it callsObject
's implementation of theclone
method by callingsuper.clone
, which creates and initializes aStack
object. At this point, the original stack and its clone refer to the same list. Next, the method clones the list. Note that the method returns aStack
, which is a subclass of the type returned by theObject.clone
method.
Note: Theclone
method should never usenew
to create the clone and should not call constructors. Instead, the method should callsuper.clone
, which creates an object of the correct type and allows the hierarchy of superclasses to perform the copying necessary to get a proper clone.
Theequals
method compares two objects for equality and returnstrue
if they are equal. Theequals
method provided in theObject
class uses the identity operator (==
) to determine whether two objects are equal. If the objects compared are the exact same object, the method returnstrue
.However, for some classes, two distinct objects of that type might be considered equal if they contain the same information. Consider this code that tests two
Integer
s,one
andanotherOne
, for equality:This program displaysInteger one = new Integer(1); Integer anotherOne = new Integer(1); if (one.equals(anotherOne)) { System.out.println("objects are equal"); }objects are equal
even thoughone
andanotherOne
reference two distinct objects. They are considered equal because the objects compared contain the same integer value.You should override the
equals
method only if the identity operator is not appropriate for your class. If you overrideequals
, overridehashCode
as well.The value returned by
hashCode
is an int that maps an object into a bucket in a hash table. An object must always produce the same hash code. However, objects can share hash codes (they aren't necessarily unique). Writing a "correct" hashing function is easy always return the same hash code for the same object. Writing an "efficient" hashing function one that provides a sufficient distribution of objects over the buckets is difficult and is outside the scope of this tutorial.Even so, the hashing function for some classes is relatively obvious. For example, an obvious hash code for an
Integer
object is its integer value.
TheObject
class provides a method,finalize
, that cleans up an object before it is garbage collected. This method's role during garbage collection was discussed previously, in the section Cleaning Up Unused Objects. Thefinalize
method is called automatically by the system, and most classes do not need to override it. Thus, you can generally ignore this method.
TheObject
'stoString
method returns aString
representation of the object. You can usetoString
along withSystem.out.println
to display a text representation of an object, such as an instance ofDouble
:TheSystem.out.println(new Double(Math.PI).toString());String
representation for an object depends entirely on the object. TheString
representation of aDouble
object is the double value displayed as text. Thus, the previous line of code displays3.14159
.The
toString
method is very useful for debugging. You should override this method in all your classes.
ThegetClass
method returns a runtime representation of the class of an object. This method returns aClass
object, which you can query for information about the class, such as its name, its superclass, and the names of the interfaces it implements. You cannot overridegetClass
. The following method gets and displays the class name of an object:One handy use of avoid PrintClassName(Object obj) { System.out.println("The Object's class is " + obj.getClass().getName()); }Class
object is to create a new instance of a class without knowing what the class is at compile time. The following sample method creates a new instance of the same class asobj
, which can be any class that inherits fromObject
:If you already know the name of the class, you can also get aObject createNewInstanceOf(Object obj) { return obj.getClass().newInstance(); }Class
object from a class name. The following two lines are equivalent ways to get aClass
object for theString
class:The first is more efficient than the second.String.class Class.forName("String")
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.