|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
TheObjectclass sits at the top of the class hierarchy tree in the Java platform. Every class in the Java system is a descendent, direct or indirect, of the
Objectclass. 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.Your classes may want to override the following
Objectmethods. Theequals/hashCodeare listed together as they must be overridden together.Your class cannot override these
cloneequals/hashCodefinalizetoStringObjectmethods (they are final):
getClassnotifynotifyAllwait
clone MethodYou use theclonemethod 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 if the object on whichclonewas invoked implements theCloneableinterface, and throws aCloneNotSupportedExceptionif it does not. Note thatObjectitself does not implementCloneable, so subclasses ofObjectthat don't explicitly implement the interface are not cloneable.If the object on which
clonewas invoked does implement theCloneableinterface,Object's implementation of theclonemethod 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 of
Object'sclonemethod works just fine. Other classes need to overridecloneto get correct behavior.Consider the
Stackclass, which contains a member variable that refers to aVector. IfStackrelies onObject's implementation ofclone, then the original stack and its clone will refer to the same vector. Changing one stack will change the other, which is undesirable behavior.Here then is an appropriate implementation of
clonefor ourStackclass, which clones the vector to ensure that the original stack and its clone do not refer to the same vector (changes are indicated with a change in font):The implementation forpublic class Stack implements Cloneable { private Vector items; // code for Stack's methods and constructor not shown protected Object clone() { try { Stack s = (Stack)super.clone(); // clone the stack s.items = (Vector)items.clone(); // clone the vector return s; // return the clone } catch (CloneNotSupportedException e) { // this shouldn't happen because Stack is Cloneable throw new InternalError(); } } }Stack'sclonemethod is relatively simple: It callssuper.clone, whichStackinherits fromObjectand which creates and initializes an instance of the correct type. At this point, the original stack and its clone refer to the same vector. Next the method clones the vector.Be careful:
cloneshould never usenewto 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.
equals and hashCode MethodsYou must override theequalsandhashCodemethods together.The
equalsmethod compares two objects for equality and returnstrueif they are equal. Theequalsmethod provided in theObjectclass uses the identity function to determine if 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
Integers,oneandanotherOne, for equality:This program displaysInteger one = new Integer(1), anotherOne = new Integer(1); if (one.equals(anotherOne)) System.out.println("objects are equal");objects are equaleven thoughoneandanotherOnereference two distinct objects. They are considered equal because the objects compared contain the same integer value.Your classes should only override the
equalsmethod if the identity function is not appropriate for your class. If you overrideequals, then overridehashCodeas well.The value returned by
hashCodeis anintthat 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 out of the scope of the tutorial.Even so, the hashing function for some classes is relatively obvious. For example, an obvious hash code for an
Integerobject is its integer value. For an example of a class that overrides theequalsandhashCodemethods, see theBingoBallclass in BINGO!
.
finalize MethodTheObjectclass 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 Cleaning Up Unused Objects. The
finalizemethod is called automatically by the system and most classes you write do not need to override it. So you can generally ignore this method.
toString MethodObject'stoStringmethod returns aStringrepresentation of the object. You can usetoStringalong withSystem.out.printlnto display a text representation of an object, such as the current thread:TheSystem.out.println(Thread.currentThread().toString());Stringrepresentation for an object depends entirely on the object. TheStringrepresentation of anIntegerobject is the integer value displayed as text. TheStringrepresentation of aThreadobject contains various attributes about the thread, such as its name and priority. For example, the previous line of code displays the following output:TheThread[main,5,main]toStringmethod is very useful for debugging. It behooves you to override this method in all your classes.
getClass MethodThegetClassmethod is a final method that returns a runtime representation of the class of an object. This method returns aClassobject.Once you have a
Classobject you can query it for various information about the class, such as its name, its superclass, and the names of the interfaces that it implements. 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()); }Classobject 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(which means that it could be any class):Object createNewInstanceOf(Object obj) { return obj.getClass().newInstance(); }
Note: If you already know the name of the class, you can also get aClassobject from a class name. The two lines shown here are equivalent ways to get aClassobject for theStringclass:The first is more efficient than the second.String.classorClass.forName("String")
notify, notifyAll,
and wait MethodsYou cannot overrideObject'snotifyandnotifyAllmethods and its three versions ofwait. This is because they are critical for ensuring that threads are synchronized. The use of these methods is covered in Threads: Doing Two or More Tasks At Once. Take particular note of the section titled Synchronizing Threads
.
|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2004 Sun Microsystems, Inc. All rights reserved.