java.lang.Object
None
Too many to be listed here
None
JDK 1.0 or later
The Object class is the ultimate superclass of all other classes in Java. Because every other class is a subclass of Object, all of the methods accessible from Object are inherited by every other class. In other words, all objects in Java, including arrays, have access to implementations of the methods in Object.
The methods of Object provide some basic object functionality. The equals() method compares two objects for equality, while the hashCode() method returns a hashcode for an object. The getClass() method returns the Class object associated with an object. The wait(), notify(), and notifyAll() methods support thread synchronization for an object. The toString() method provides a string representation of an object.
Some of these methods should be overridden by subclasses of Object. For example, every class should provide its own implementation of the toString() method, so that it can provide an appropriate string representation.
Although it is possible to create an instance of the Object class, this is rarely done because it is more useful to create specialized objects. However, it is often useful to declare a variable that contains a reference to an Object because such a variable can contain a reference to an object of any other class.
public class java.lang.Object { // Constructors public Object(); // Public Instance Methods public boolean equals(Object obj); public final native Class getClass(); public native int hashCode(); public final native void notify(); public final native void notifyAll(); public String toString(); public final native void wait(); public final native void wait(long millis); public final native void wait(long millis, int nanos); // Protected Instance Methods protected native Object clone(); protected void finalize() throws Throwable; }
Creates an instance of the Object class.
The object to be compared with this object.
true if the objects are equal; false if they are not.
The equals() method of Object returns true if the obj parameter refers to the same object as the object this method is associated with. This is equivalent to using the == operator to compare two objects.
Some classes, such as String, override the equals() method to provide a comparison based on the contents of the two objects, rather than on the strict equality of the references. Any subclass can override the equals() method to implement an appropriate comparison, as long as the overriding method satisfies the following rules for an equivalence relation:
A reference to the Class object that describes the class of this object.
The getClass() method of Object returns the Class object that describes the class of this object. This method is final, so it cannot be overridden by subclasses.
A relatively unique value that should be the same for all objects that are considered equal.
The hashCode() method of Object calculates a hashcode value for this object. The method returns an integer value that should be relatively unique to the object. If the equals() method for the object bases its result on the contents of the object, the hashcode() method should also base its result on the contents. The hashCode() method is provided for the benefit of hashtables, which store and retrieve elements using key values called hashcodes. The internal placement of a particular piece of data is determined by its hashcode; hashtables are designed to use hashcodes to provide efficient retrieval.
The java.util.Hashtable class provides an implementation of a hashtable that stores values of type Object. Each object is stored in the hashtable based on the hash code of its key object. It is important that each object have the most unique hash code possible. If two objects have the same hash code but they are not equal (as determined by equals()), a Hashtable that stores these two objects may need to spend additional time searching when it is trying to retrieve objects. The implementation of hashCode() in Object tries to make sure that every object has a distinct hash code by basing its result on the internal representation of a reference to the object.
Some classes, such as String, override the hashCode() method to produce values based on the contents of individual objects, instead of the objects themselves. In other words, two String objects that contain the exact same strings have the same hash code. If String did not override the hashCode() method inherited from Object, these two String objects would have different hash code values and it would be impossible to use String objects as keys for hashtables.
Any subclass can override the hashCode() method to implement an appropriate way of producing hash code values, as long as the overriding method satisfies the following rules:
In general, if a subclass overrides the equals() method of Object, it should also override the hashCode() method.
If the method is called from a thread that does not hold this object's lock.
The notify() method wakes up a thread that is waiting to return from a call to this object's wait() method. The awakened thread can resume executing as soon as it regains this object's lock. If more than one thread is waiting, the notify() method arbitrarily awakens just one of the threads.
The notify() method can be called only by a thread that is the current owner of this object's lock. A thread holds the lock on this object while it is executing a synchronized instance method of the object or executing the body of a synchronized statement that synchronizes on the object. A thread can also hold the lock for a Class object if it is executing a synchronized static method of that class.
This method is final, so it cannot be overridden by subclasses.
If the method is called from a thread that does not hold this object's lock.
The notifyAll() method wakes up all the threads that are waiting to return from a call to this object's wait() method. Each awakened thread can resume executing as soon as it regains this object's lock.
The notifyAll() method can be called only by a thread that is the current owner of this object's lock. A thread holds the lock on this object while it is executing a synchronized instance method of the object or executing the body of a synchronized statement that synchronizes on the object. A thread can also hold the lock for a Class object if it is executing a synchronized static method of that class.
This method is final, so it cannot be overridden by subclasses.
The string representation of this object.
The toString() method of Object returns a generic string representation of this object. The method returns a String that consists of the object's class name, an "at" sign, and the unsigned hexadecimal representation of the value returned by the object's hashCode() method.
Many classes override the toString() method to provide a string representation that is specific to that type of object. Any subclass can override the toString() method; the overriding method should simply return a String that represents the contents of the object with which it is associated.
If the method is called from a thread that does not hold this object's lock.
If another thread interrupted this thread.
The wait() method causes a thread to wait until it is notified by another thread to stop waiting. When wait() is called, the thread releases its lock on this object and waits until another thread notifies it to wake up through a call to either notify() or notifyAll(). After the thread is awakened, it has to regain the lock on this object before it can resume executing.
The wait() method can be called only by a thread that is the current owner of this object's lock. A thread holds the lock on this object while it is executing a synchronized instance method of the object or executing the body of a synchronized statement that synchronizes on the object. A thread can also hold the lock for a Class object if it is executing a synchronized static method of that class.
This method is final, so it cannot be overridden by subclasses.
public final native void wait(long timeout) throws InterruptedException
The maximum number of milliseconds to wait.
If the method is called from a thread that does not hold this object's lock.
If another thread interrupted this thread.
The wait() method causes a thread to wait until it is notified by another thread to stop waiting or until the specified amount of time has elapsed, whichever comes first. When wait() is called, the thread releases its lock on this object and waits until another thread notifies it to wake up through a call to either notify() or notifyAll(). If the thread is not notified within the specified timeout period, it is automatically awakened when that amount of time has elapsed. If timeout is zero, the thread waits indefinitely, just as if wait() had been called without a timeout argument. After the thread is awakened, it has to regain the lock on this object before it can resume executing.
The wait() method can be called only by a thread that is the current owner of this object's lock. A thread holds the lock on this object while it is executing a synchronized instance method of the object or executing the body of a synchronized statement that synchronizes on the object. A thread can also hold the lock for a Class object if it is executing a synchronized static method of that class.
This method is final, so it cannot be overridden by subclasses.
public final native void wait(long timeout, int nanos) throws InterruptedException
The maximum number of milliseconds to wait.
An additional number of nanoseconds to wait.
If the method is called from a thread that does not hold this object's lock.
If another thread interrupted this thread.
The wait() method causes a thread to wait until it is notified by another thread to stop waiting or until the specified amount of time has elapsed, whichever comes first. When wait() is called, the thread releases its lock on this object and waits until another thread notifies it to wake up through a call to either notify() or notifyAll(). If the thread is not notified within the specified time period, it is automatically awakened when that amount of time has elapsed. If timeout and nanos are zero, the thread waits indefinitely, just as if wait() had been called without any arguments. After the thread is awakened, it has to regain the lock on this object before it can resume executing.
The wait() method can be called only by a thread that is the current owner of this object's lock. A thread holds the lock on this object while it is executing a synchronized instance method of the object or executing the body of a synchronized statement that synchronizes on the object. A thread can also hold the lock for a Class object if it is executing a synchronized static method of that class.
Note that Sun's reference implementation of Java does not attempt to implement the precision implied by this method. Instead, it rounds to the nearest millisecond (unless timeout is 0, in which case it rounds up to 1 millisecond) and calls wait(long).
This method is final, so it cannot be overridden by subclasses.
A clone of this object.
If there is not enough memory to create the new object.
If the object is of a class that does not support clone().
A clone of an object is another object of the same type that has all of its instance variables set to the same values as the object being cloned. In other words, a clone is an exact copy of the original object.
The clone() method of Object creates a new object that is a clone of this object. No constructor is used in creating the clone. The clone() method only clones an object if the class of that object indicates that its instances can be cloned. A class indicates that its objects can be cloned by implementing the Cloneable interface.
Although array objects do not implement the Cloneable interface, the clone() method works for arrays. The clone of an array is an array that has the same number of elements as the original array, and each element in the clone array has the same value as the corresponding element in the original array. Note that if an array element contains an object reference, the clone array contains a reference to the same object, not a copy of the object.
A subclass of Object can override the clone() method in Object to provide any additional functionality that is needed. For example, if an object contains references to other objects, the clone() method should recursively call the clone() methods of all the referenced objects. An overriding clone() method can throw a CloneNotSupportedException to indicate that particular objects cannot be cloned.
For any reason that suits an overriding implementation of this method.
The finalize() method is called by the garbage collector when it decides that an object can never be referenced again. The method gives an object a chance to perform any cleanup operations that are necessary before it is destroyed by the garbage collector.
The finalize() method of Object does nothing. A subclass overrides the finalize() method to perform any necessary cleanup operations. The overriding method should call super.finalize() as the very last thing it does, so that any finalize() method in the superclass is called.
When the garbage collector calls an object's finalize() method, the garbage collector does not immediately destroy the object because the finalize() method might do something that results in a reference to the object. Thus the garbage collector waits to destroy the object until it can again prove it is safe to do so. The next time the garbage collector decides it is safe to destroy the object, it does so without calling finalize() again. In other words, the garbage collector never calls the finalize() method more than once for a particular object.
A finalize() method can throw any kind of exception. An exception causes the finalize() method to stop running. The garbage collector then catches and ignores the exception, so it has no further effect on a program.