public class

Object

java.lang.Object
Known Direct Subclasses
Known Indirect Subclasses

Class Overview

The root class of the Java class hierarchy. All non-primitive types (including arrays) inherit either directly or indirectly from this class.

Object provides some fundamental methods for accessing the Class of an object, getting its hashCode(), or checking whether one object equals(Object) another. The toString() method can be used to convert an object reference into a printable string and is often overridden in subclasses.

The wait() and notify() methods provide a foundation for synchronization, acquiring and releasing an internal monitor associated with each Object.

Summary

Public Constructors
Object()
Constructs a new instance of Object.
Public Methods
boolean equals(Object o)
Compares this instance with the specified object and indicates if they are equal.
final Class<? extends Object> getClass()
Returns the unique instance of Class which represents this object's class.
int hashCode()
Returns an integer hash code for this object.
final void notify()
Causes a thread which is waiting on this object's monitor (by means of calling one of the wait() methods) to be woken up.
final void notifyAll()
Causes all threads which are waiting on this object's monitor (by means of calling one of the wait() methods) to be woken up.
String toString()
Returns a string containing a concise, human-readable description of this object.
final void wait(long millis, int nanos)
Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object or until the specified timeout expires.
final void wait(long millis)
Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object or until the specified timeout expires.
final void wait()
Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object.
Protected Methods
Object clone()
Creates and returns a copy of this Object.
void finalize()
Is called before the object's memory is being reclaimed by the VM.

Public Constructors

public Object ()

Constructs a new instance of Object.

Public Methods

public boolean equals (Object o)

Compares this instance with the specified object and indicates if they are equal. In order to be equal, o must represent the same object as this instance using a class-specific comparison. The general contract is that this comparison should be both transitive and reflexive.

The implementation in Object returns true only if o is the exact same object as the receiver (using the == operator for comparison). Subclasses often implement equals(Object) so that it takes into account the two object's types and states.

The general contract for the equals(Object) and hashCode() methods is that if equals returns true for any two objects, then hashCode() must return the same value for these objects. This means that subclasses of Object usually override either both methods or none of them.

Parameters
o the object to compare this instance with.
Returns
  • true if the specified object is equal to this Object; false otherwise.
See Also

public final Class<? extends Object> getClass ()

Returns the unique instance of Class which represents this object's class. Note that getClass() is a special case in that it actually returns Class where Foo is the erasure of the type of expression getClass() was called upon.

As an example, the following code actually compiles, although one might think it shouldn't:

 List l = new ArrayList();
 Class c = l.getClass();
 

Returns
  • this object's Class instance.

public int hashCode ()

Returns an integer hash code for this object. By contract, any two objects for which equals(Object) returns true must return the same hash code value. This means that subclasses of Object usually override both methods or neither method.

Returns
  • this object's hash code.
See Also

public final void notify ()

Causes a thread which is waiting on this object's monitor (by means of calling one of the wait() methods) to be woken up. If more than one thread is waiting, one of them is chosen at the discretion of the virtual machine. The chosen thread will not run immediately. The thread that called notify() has to release the object's monitor first. Also, the chosen thread still has to compete against other threads that try to synchronize on the same object.

This method can only be invoked by a thread which owns this object's monitor. A thread becomes owner of an object's monitor

  • by executing a synchronized method of that object;
  • by executing the body of a synchronized statement that synchronizes on the object;
  • by executing a synchronized static method if the object is of type Class.

public final void notifyAll ()

Causes all threads which are waiting on this object's monitor (by means of calling one of the wait() methods) to be woken up. The threads will not run immediately. The thread that called notify() has to release the object's monitor first. Also, the threads still have to compete against other threads that try to synchronize on the same object.

This method can only be invoked by a thread which owns this object's monitor. A thread becomes owner of an object's monitor

  • by executing a synchronized method of that object;
  • by executing the body of a synchronized statement that synchronizes on the object;
  • by executing a synchronized static method if the object is of type Class.

Throws
IllegalMonitorStateException if the thread calling this method is not the owner of this object's monitor.

public String toString ()

Returns a string containing a concise, human-readable description of this object. Subclasses are encouraged to override this method and provide an implementation that takes into account the object's type and data. The default implementation simply concatenates the class name, the '@' sign and a hexadecimal representation of the object's hashCode(), that is, it is equivalent to the following expression:

 getClass().getName() + '@' + Integer.toHexString(hashCode())
 

Returns
  • a printable representation of this object.

public final void wait (long millis, int nanos)

Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object or until the specified timeout expires. This method can only be invoked by a thread that owns this object's monitor; see notify() on how a thread can become the owner of a monitor.

A waiting thread can be sent interrupt() to cause it to prematurely stop waiting, so wait should be called in a loop to check that the condition that has been waited for has been met before continuing.

While the thread waits, it gives up ownership of this object's monitor. When it is notified (or interrupted), it re-acquires the monitor before it starts running.

Parameters
millis the maximum time to wait in milliseconds.
nanos the fraction of a millisecond to wait, specified in nanoseconds.
Throws
IllegalArgumentException if millis < 0, nanos < 0 or nanos > 999999.
IllegalMonitorStateException if the thread calling this method is not the owner of this object's monitor.
InterruptedException if another thread interrupts this thread while it is waiting.

public final void wait (long millis)

Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object or until the specified timeout expires. This method can only be invoked by a thread which owns this object's monitor; see notify() on how a thread can become the owner of a monitor.

A waiting thread can be sent interrupt() to cause it to prematurely stop waiting, so wait should be called in a loop to check that the condition that has been waited for has been met before continuing.

While the thread waits, it gives up ownership of this object's monitor. When it is notified (or interrupted), it re-acquires the monitor before it starts running.

Parameters
millis the maximum time to wait in milliseconds.
Throws
IllegalArgumentException if millis < 0.
IllegalMonitorStateException if the thread calling this method is not the owner of this object's monitor.
InterruptedException if another thread interrupts this thread while it is waiting.

public final void wait ()

Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object. This method can only be invoked by a thread which owns this object's monitor; see notify() on how a thread can become the owner of a monitor.

A waiting thread can be sent interrupt() to cause it to prematurely stop waiting, so wait should be called in a loop to check that the condition that has been waited for has been met before continuing.

While the thread waits, it gives up ownership of this object's monitor. When it is notified (or interrupted), it re-acquires the monitor before it starts running.

Throws
IllegalMonitorStateException if the thread calling this method is not the owner of this object's monitor.
InterruptedException if another thread interrupts this thread while it is waiting.

Protected Methods

protected Object clone ()

Creates and returns a copy of this Object. The default implementation returns a so-called "shallow" copy: It creates a new instance of the same class and then copies the field values (including object references) from this instance to the new instance. A "deep" copy, in contrast, would also recursively clone nested objects. A subclass that needs to implement this kind of cloning should call super.clone() to create the new instance and then create deep copies of the nested, mutable objects.

Returns
  • a copy of this object.
Throws
CloneNotSupportedException if this object's class does not implement the Cloneable interface.

protected void finalize ()

Is called before the object's memory is being reclaimed by the VM. This can only happen once the VM has detected, during a run of the garbage collector, that the object is no longer reachable by any thread of the running application.

The method can be used to free system resources or perform other cleanup before the object is garbage collected. The default implementation of the method is empty, which is also expected by the VM, but subclasses can override finalize() as required. Uncaught exceptions which are thrown during the execution of this method cause it to terminate immediately but are otherwise ignored.

Note that the VM does guarantee that finalize() is called at most once for any object, but it doesn't guarantee when (if at all) finalize() will be called. For example, object B's finalize() can delay the execution of object A's finalize() method and therefore it can delay the reclamation of A's memory. To be safe, use a ReferenceQueue, because it provides more control over the way the VM deals with references during garbage collection.

Throws
Throwable any exception which is raised during finalization; these are ignored by the virtual machine.