Fundamentals 4 min read

Understanding Java’s Object Class: 12 Essential Methods Explained

This article breaks down Java's Object class, detailing its twelve core methods—including equals, hashCode, clone, getClass, notify, notifyAll, and the various wait overloads—providing clear explanations, usage examples, and best‑practice guidelines for developers.

Mike Chen's Internet Architecture
Mike Chen's Internet Architecture
Mike Chen's Internet Architecture
Understanding Java’s Object Class: 12 Essential Methods Explained

Object class skeleton

public class Object {
    public Object() {}
    public final native Class<?> getClass();
    public native int hashCode();
    public boolean equals(Object obj);
    protected native Object clone() throws CloneNotSupportedException;
    public String toString();
    public final native void notify();
    public final native void notifyAll();
    public final native void wait(long timeout) throws InterruptedException;
    public final native void wait(long timeout, int nanos) throws InterruptedException;
    public final native void wait() throws InterruptedException;
    protected void finalize() throws Throwable {}
}

equals(Object obj)

Compares two objects for reference equality; it returns true only when both references point to the same instance. Most classes override this method to perform content‑based comparison.

Object obj1 = new Object();
Object obj2 = new Object();
Object obj3 = obj1;
System.out.println(obj1.equals(obj2)); // false
System.out.println(obj1.equals(obj3)); // true

Typical override pattern:

@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }
    if (obj == null || getClass() != obj.getClass()) {
        return false;
    }
    MyClass other = (MyClass) obj;
    return Objects.equals(this.someField, other.someField);
}

hashCode()

Returns an integer hash code for the object, usually derived from the object's memory address. Collections such as HashMap rely on this value to locate storage buckets. When equals is overridden, hashCode must also be overridden to maintain the contract that equal objects have equal hash codes.

@Override
public int hashCode() {
    return Objects.hash(someField);
}

clone()

Creates and returns a shallow copy of the current object. A class must implement the Cloneable interface; otherwise, clone() throws CloneNotSupportedException.

class MyClass implements Cloneable {
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

getClass()

Returns the runtime class of the object.

Object obj = new Object();
System.out.println(obj.getClass()); // class java.lang.Object

notify() , notifyAll() and wait()

notify()

wakes a single thread waiting on the object's monitor; notifyAll() wakes all waiting threads. The wait() family causes the current thread to pause until it is notified or the optional timeout expires.

synchronized (obj) {
    obj.wait(); // wait indefinitely
}

synchronized (obj) {
    obj.notify(); // wake one waiting thread
}

synchronized (obj) {
    obj.notifyAll(); // wake all waiting threads
}

synchronized (obj) {
    obj.wait(1000); // wait up to 1 second
}

These methods are fundamental for building thread‑safe coordination primitives in Java.

JavaSynchronizationfinalizecloneequalshashCodeObject
Mike Chen's Internet Architecture
Written by

Mike Chen's Internet Architecture

Over ten years of BAT architecture experience, shared generously!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.