Object Class
As we discussed earlier in this chapter, all classes in Java inherit directly or indirectly from the Object class (package java.lang), so its 11 methods are inherited by all other classes. Figure 9.18 summarizes Object's methods.
Method |
Description |
---|---|
clone |
This protected method, which takes no arguments and returns an Object reference, makes a copy of the object on which it is called. When cloning is required for objects of a class, the class should override method clone as a public method and should implement interface Cloneable (package java.lang). The default implementation of this method performs a so-called shallow copyinstance variable values in one object are copied into another object of the same type. For reference types, only the references are copied. A typical overridden clone method's implementation would perform a deep copy that creates a new object for each reference type instance variable. There are many subtleties to overriding method clone. You can learn more about cloning in the following article: java.sun.com/developer/JDCTechTips/2001/tt0306.html |
equals |
This method compares two objects for equality and returns TRue if they are equal and false otherwise. The method takes any Object as an argument. When objects of a particular class must be compared for equality, the class should override method equals to compare the contents of the two objects. The method's implementation should meet the following requirements:
A class that overrides equals should also override hashCode to ensure that equal objects have identical hashcodes. The default equals implementation uses operator == to determine whether two references refer to the same object in memory. Section 29.3.3 demonstrates class String's equals method and differentiates between comparing String objects with == and with equals. |
finalize |
This protected method (introduced in Section 8.10 and Section 8.11) is called by the garbage collector to perform termination housekeeping on an object just before the garbage collector reclaims the object's memory. It is not guaranteed that the garbage collector will reclaim an object, so it cannot be guaranteed that the object's finalize method will execute. The method must specify an empty parameter list and must return void. The default implementation of this method serves as a placeholder that does nothing. |
getClass |
Every object in Java knows its own type at execution time. Method getClass (used in Section 10.5 and Section 21.3) returns an object of class Class (package java.lang) that contains information about the object's type, such as its class name (returned by Class method getName). You can learn more about class Class in the online API documentation at java.sun.com/j2se/5.0/docs/api/java/lang/Class.html. |
hashCode |
A hashtable is a data structure (discussed in Section 19.10) that relates one object, called the key, to another object, called the value. When initially inserting a value into a hashtable, the key's hashCode method is called. The hashcode value returned is used by the hashtable to determine the location at which to insert the corresponding value. The key's hashcode is also used by the hashtable to locate the key's corresponding value. |
notify, notifyAll, wait |
Methods notify, notifyAll and the three overloaded versions of wait are related to multithreading, which is discussed in Chapter 23. In J2SE 5.0, the multithreading model has changed substantially, but these features continue to be supported. |
toString |
This method (introduced in Section 9.4.1) returns a String representation of an object. The default implementation of this method returns the package name and class name of the object's class followed by a hexadecimal representation of the value returned by the object's hashCode method. |
We discuss several of Object methods throughout this book (as indicated in the table). You can learn more about Object's methods in Object's online API documentation and in The Java Tutorial at the following sites:
java.sun.com/j2se/5.0/docs/api/java/lang/Object.html
java.sun.com/docs/books/tutorial/java/javaOO/objectclass
Recall from Chapter 7 that arrays are objects. As a result, like all other objects, an array inherits the members of class Object. Note that arrays have an overridden clone method that copies the array. However, if the array stores references to objects, the objects are not copied. For more information about the relationship between arrays and class Object, please see Java Language Specification, Chapter 10, at
java.sun.com/docs/books/jls/second_edition/html/arrays.doc.html