![]() |
|||
![]() ![]() |
![]() |
![]()
|
![]() |
Listing 3-1 is a program that demonstrates most of these methods. The static printRTTI(Object o) method is the heart of it. This method checks all the possible type information about the object that it has passed and prints it on System.out. This can be very useful for debugging. The main() method tests the program on two objects: an Integer and a DataOutputStream. The three methods printHierarchy(), printInterfaces(), and printClassLoader() break up the code to make it a little more legible. Each one handles a particular aspect of the runtime type. Listing 3-1 RTTI import java.io.*; public class RTTI { public static void main(String args[]) { printRTTI(new Integer(7)); printRTTI(new DataOutputStream(System.out)); } public static void printRTTI(Object o) { if (o == null) { System.out.println("This object is null"); return; } Class c = o.getClass(); printHierarchy(c); printInterfaces(c); printClassLoader(c); } static void printHierarchy(Class c) { System.out.println(c.getName()); while ((c = c.getSuperclass()) != null) { System.out.println("extends " + c.getName()); } } static void printInterfaces(Class c) { Class[] ci = c.getInterfaces(); if (ci.length > 0) { if (c.isInterface()) { for (int i = 0; i < ci.length; i++) { System.out.println("extends" + ci[i].getName()); } } else { System.out.println("implements "); for (int i = 0; i < ci.length; i++) { System.out.println(ci[i].getName() + ","); } } } } static void printClassLoader(Class c) { ClassLoader cl = c.getClassLoader(); if (cl != null) System.out.println("This object was loaded by " + cl); } } The Object ClassNow that you know that classes are objects, Ill confuse you a little more by telling you that objects are classes, too. As with the Class class, there is, however, a difference between the big O Object (which is a class) and the little o object (which is an object). The java.lang.Object class is the common superclass for all Java classes. All classes eventually extend java.lang.Object. Thus, all classes have access to the methods of java.lang.Object. The primary purpose of java.lang.Object is to provide several useful methods that the programmer can count on all classes having. These are clone(), equals(), finalize(), getClass(), hashCode(), notify(), notifyAll(), toString(), and wait(). Furthermore theres a single constructor, Object(), but youll rarely (if ever) call it directly. What these methods have in common is that they represent internal details of Java objects, not anything external to Java like a window, a mouse, a motorcycle, a supernova, or anything else that exists outside the virtual machine. Objects normally represent real world entities. However, the java.lang.Object class is something of a meta-class that is, a class that represents objects. The clone() method copies objects. The equals() method compares objects. The getClass() method returns the class of an object. The hashCode() method computes a unique integer for an object. The toString() method creates a string representation of an object. The notify(), notifyAll(), and the wait() methods interface between objects and threads. What all these methods have in common is that they treat objects as computer-based abstractions, not as real world things. Lets take a closer look at these methods. CloningThe clone() method makes a bitwise copy of an object in memory and returns the copy. In other words, it creates a new instance of the objects class and copies the values of each field of the object into the new object. It copies all fields, whether theyre public, private, protected, or package protected. Not all objects can be cloned. In fact, by default an object may not be cloned. Only objects that implement the java.lang.Cloneable interface may be cloned. The Cloneable interface does not actually declare any methods. It just tells the clone method in java.lang.Object that its okay to clone this object. If you try to clone an object that does not implement the Cloneable interface, a CloneNotSupportedException is thrown.
|
![]() |
|