• No results found

The Methods of the Object Class

Object is a parent of every class, so the methods in the Object class are inher- ited by every Java object. Therefore, the methods in the Object class can be invoked on any Java object, no matter what class type the object is.

The following list contains the signatures of the methods in the Object class and a description of what each method does:

public final Class getClass(). Every class used in a Java program is loaded by the JVM. When the JVM loads a class, the information about the class is stored in a Class object. Use this method to obtain a reference to the Class object of the object you invoke the method on.

public int hashcode(). This method returns a hashcode value for the object, which is useful when working with hash tables and other data structures that use hashing.

public boolean equals(Object x). Use this method to check if two objects are equal to each other. This method is often overridden (method over- riding is discussed in the next section) and allows a class to determine what it means for two objects of that type to be equal. Note if two objects are equal as determined by this method, then the hashcode() method of the two objects should generate the same hash code.

protected Object clone() throws CloneNotSupportedException. The clone method is used to create a copy of an object. The exception occurs when the object being cloned does not support cloning. The details of cloning objects are not discussed in this book.

public String toString(). This method returns a string representation of the object. Representing an object as a String can be helpful for debug- ging or testing purposes. The Java documentation recommends that you add the toString() method to all of your classes, a widely used practice in Java programming.

protected void finalize() throws Throwable. This method is invoked on an object right before the object is to be garbage collected. The finalize() method allows for an object to free up any resources or perform any nec- essary cleanup before the object is removed from memory.

public final void wait() throws InterruptedException. The wait() method has two other overloaded versions in the Object class. Invoking wait() on an object causes the current thread to stop executing until some other thread invokes notify() on the same object. The wait() and notify() methods are used for thread synchronization.

public final void notify(). There is also a notifyAll() method in the Object class. These methods are used to restart any threads that were blocked by invoking one of the wait() methods on the object.

The final methods in the Object class, such as wait() and notify(), cannot be changed by the child classes of Object; however, the nonfinal methods are intended to be included in classes that want or need to change the default behavior of the corresponding method in the Object class.

For example, the default behavior of the toString() method in Object is to print out the name of the class, followed by the @ symbol, followed by the hashcode value. If you do not want this behavior, add toString() to your class and generate any string you like.

The following ToStringDemo program invokes the toString() method of the following Radio class. Notice the Radio class does not contain a toString() method but inherits the default toString() method in Object.

Study the ToStringDemo program carefully and try to determine the output. (Note that I added something that we have not discussed yet, printing out just a reference.)

public class Radio {

public int volume; public double channel; public char band;

public Radio(int v, double c, char b) { volume = v; channel = c; band = b; } }

public class ToStringDemo {

public static void main(String [] args) {

Radio radio = new Radio(7, 100.3, ‘F’);

System.out.println(“toString returns “ + radio.toString());

System.out.println(“Just printing the reference: “ + radio); }

}

Figure 6.2 shows the output of the ToStringDemo.

The toString() method invoked on the radio object created the following String:

Radio@1ea2dfe

The output of printing out the reference generated the same String:

Just printing the reference: Radio@1ea2dfe

The toString() method was invoked implicitly by the JVM when the reference was concatenated with a String.

When the radio reference is concatenated to the string “Just printing the reference: “, the reference needs to be converted to a String before the concatenation can occur. Notice that the toString() method is invoked automatically. Because every object in Java is a child of Object, every object has a toString() method, and the JVM will invoke toString() implicitly anytime the object needs to be converted to a String.

Figure 6.2 The output of the ToStringDemo program.

The default toString() method, although sometimes useful, is not very excit- ing. Instead of using the default version, the Radio class can include its own toString() method, allowing the Radio class to create its own string representa- tion of a Radio object. Adding toString() to the Radio class replaces the toString() method in Object. This is an example of method overriding, which we will now discuss.