Search This Blog

Tuesday, 6 December 2011

Object Class functions in java

Object Class
Object Class is the Super class for all classes in JAVA.The Object class defines the basic state and behavior that all objects must have, such as the ability to compare oneself to another object, to convert to a string, to wait on a condition variable, to notify other objects that a condition variable has changed, and to return the object's class.
The Object Class Functions:
This function is used to compare two objects whether they have equivalent values are not.
It returns true if they are equal else returns false.
The getClass() Method:
The getClass method is a final method (cannot be overridden)
This method returns a Class object.With this class you can get the information of object like name, its super class, and the names of the interfaces that it implements.
use of the getClass method is to create a new instance of a class without knowing what the class is at compile time. This sample method creates a new instance of the same class as obj which can be any class that inherits from Object (which means that it could be any class):
Object createNewInstanceOf(Object obj) {
return obj.getClass().newInstance();
The toString Method
Object's toString method returns a String representation of the object. You can use toString to display an object.
The Object class also provides five methods that are critical when writing multithreaded Java programs:
• notify
• notifyAll
• wait (three versions)
Returns a hash code value for the object.
Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

No comments:

Post a Comment