Object Class Methods in Java
Last Updated : 5 Aug 2025
In Java, Object class belongs to the java.lang package. It is the parent class of all the Java classes, so it sits at the top of the class hierarchy. It means that each Java class directly or indirectly inherits the methods of the Object class. These methods provide fundamental functionalities that are common to all Java objects.
Object Class Methods
The following figure shows the some commonly used methods of the Object class.

Object.getClass() Method
It is the final method of the Object class that returns the runtime class of the object. We cannot override this method. Its primary use is to obtain class-specific information for an object, including its name, parent class, and any interfaces it's using.
Syntax:
Output:
Computer class java.lang.String
Object.toString() Method
The method returns the string representation (that "textually represents" this object) of the method. By default, if not overridden, it shows a string that uniquely identifies an object in memory. For example, Demo@1a2b3c4d, where Demo is the class name.
String consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object.
In other words, this method returns a string equal to the value of:
Syntax:
Output:
Book{title='1984', author='George Orwell'}
Object.equals() Method
The method logically compares two object and check if the two objects are equal or not. Note that method does not compare reference equality.
The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).
Output:
obj1.equals(obj2): false obj1.equals(obj3): true obj2.equals(obj3): false
Object.hashCode() Method
The hashCode() method returns an integer value (hash code) that represents object. It's crucial for using objects in hash-based collections like HashMap, HashSet, and Hashtable.
The default implementation in Object typically returns a value based on the memory address of the object. It's a native method, means that it's implemented in platform-specific code, not pure Java.
We need to override hashCode() method when we try to override equals() method. It ensures that equals() objects have the same hash codes. If objects are equal, we get the same hash codes, if not equal, we get different hash code.
Output:
obj1.equals(obj2): false obj1.equals(obj3): true obj2.equals(obj3): false
Object.clone() Method
The method creates and returns (a new object) a copy of the object. The exact meaning of "copy" depends on the class of the object. By default, it performs shallow copy.
The method supports the cloning of objects. For a class to be cloneable, it must implement the Cloneable interface (a marker interface). If Cloneable interface is not implemented it throws CloneNotSupportedException.
Syntax:
To read more Object Cloning in Java
Output:
Object.finalize() Method
The method is used to perform cleanup operations (such as closing open files, releasing memory, or shutting down connections) on an object before it is removed from memory. It is invoked by the garbage collector on an object.
Syntax:
It executes when the garbage collector identifies that an object is no longer referenced by any part of the program, signifying it is eligible for reclamation.
The finalize method is never invoked more than once by a Java virtual machine for any given object.
Note: Do not rely on finalize() for critical resource cleanup.
To read more finalize() Method in Java
Output:
Object created Main method completed finalize() is called before an object is garbage collected
Object.wait() Method
It is a final method used for inter-thread communication. The current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object.
It must be called inside a synchronized block (otherwise we will get IllegalMonitorStateException). It's a method from java.lang.Object, not from Thread, so it can be used on any object.
Syntax:
The current thread must own this object's monitor. The thread releases ownership of this monitor and waits until another thread notifies threads waiting on this object's monitor to wake up either through a call to the notify method or the notifyAll() method. The thread then waits until it can re-obtain ownership of the monitor and resumes execution.
Output:
Thread waiting... Thread notifying... Thread resumed.
Object.notify() Method
The method is used to wake up a single thread that is waiting on this object's monitor. If any threads are waiting on this object, one of them is chosen to be awakened. The choice is arbitrary and occurs at the discretion of the implementation. A thread waits on an object's monitor by calling one of the wait methods.
Syntax:
The method should only be called by a thread that is the owner of this object's monitor. A thread becomes the owner of the object's monitor in one of three ways:
- By executing a synchronized instance method of that object.
- By executing the body of a synchronized statement that synchronizes on the object.
- For objects of type Class, by executing a synchronized static method of that class.
Output:
Consumer waiting... Producer trying to produce... Producer produced and notified. Consumer consumed.
Explanation
In the above program, Consumer thread waits until available is true. Producer sets available = true and calls notify() to wake up the consumer. Consumer resumes and processes the "produced" item.
Conclusion
Understanding these Object class methods is crucial for effective Java programming, especially when designing our classes, working with collections, or handling concurrent operations. Correctly overriding toString(), equals(), and hashCode() is a very common and important practice that underpins reliable object behaviour in many Java applications.
Object Class Methods MCQs
1) Every class in Java, whether explicitly extended or not, directly or indirectly inherits from the _____ class.
- String
- System
- Object
- Class
Answer: c)
Explanation: All Java classes inherit from Object.
2) The public final Class<?> getClass() method reliably tells us the exact _____ of an object.
- memory address
- runtime class
- hash code
- package name
Answer: b)
Explanation: getClass() reveals an object's exact type at runtime.
3) When overriding the equals() method, it is crucial also to override _____ to maintain its general contract.
- toString()
- clone()
- hashCode()
- finalize()
Answer: c)
Explanation: equals() and hashCode() must be consistent for correct behaviour.
4) For a class to support object cloning using the clone() method, it must implement the _____ interface.
- Serializable
- Comparable
- Runnable
- Cloneable
Answer: d)
Explanation: The Cloneable interface is required for clone() to work.
5) The finalize() method is generally _____ in modern Java due to its unpredictable nature and the availability of better resource management practices.
- recommended
- encouraged
- discouraged
- essential
Answer: c)
Explanation: finalize() is unreliable, and its use is discouraged.