Which of these method of object class is used to obtain class of an object at run time?

next → ← prev

The Object class is the parent class of all the classes in java by default. In other words, it is the topmost class of java.

The Object class is beneficial if you want to refer any object whose type you don't know. Notice that parent class reference variable can refer the child class object, know as upcasting.

Let's take an example, there is getObject() method that returns an object but it can be of any type like Employee,Student etc, we can use Object class reference to refer that object. For example:

The Object class provides some common behaviors to all the objects such as object can be compared, object can be cloned, object can be notified etc.

Which of these method of object class is used to obtain class of an object at run time?

Methods of Object class

The Object class provides many methods. They are as follows:

MethodDescription
public final Class getClass() returns the Class class object of this object. The Class class can further be used to get the metadata of this class.
public int hashCode() returns the hashcode number for this object.
public boolean equals(Object obj) compares the given object to this object.
protected Object clone() throws CloneNotSupportedException creates and returns the exact copy (clone) of this object.
public String toString() returns the string representation of this object.
public final void notify() wakes up single thread, waiting on this object's monitor.
public final void notifyAll() wakes up all the threads, waiting on this object's monitor.
public final void wait(long timeout)throws InterruptedException causes the current thread to wait for the specified milliseconds, until another thread notifies (invokes notify() or notifyAll() method).
public final void wait(long timeout,int nanos)throws InterruptedException causes the current thread to wait for the specified milliseconds and nanoseconds, until another thread notifies (invokes notify() or notifyAll() method).
public final void wait()throws InterruptedException causes the current thread to wait, until another thread notifies (invokes notify() or notifyAll() method).
protected void finalize()throws Throwable is invoked by the garbage collector before object is being garbage collected.

We will have the detailed learning of these methods in next chapters.

Next TopicObject Cloning in java

← prev next →


Which of these method of object class is used to obtain class of an object at run time?
For Videos Join Our Youtube Channel: Join Now


Feedback

  • Send your Feedback to [email protected]

Help Others, Please Share

Which of these method of object class is used to obtain class of an object at run time?
Which of these method of object class is used to obtain class of an object at run time?
Which of these method of object class is used to obtain class of an object at run time?






The Object

Which of these method of object class is used to obtain class of an object at run time?
class sits at the top of the class hierarchy tree in the Java development environment. Every class in the Java system is a descendent (direct or indirect) of the Object class. 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 equals Method

Use the equals to compare two objects for equality. This method returns true if the objects are equal, false otherwise. Note that equality does not mean that the objects are the same object. Consider this code that tests two Integers, one and anotherOne, for equality:
Integer one = new Integer(1), anotherOne = new Integer(1);

if (one.equals(anotherOne))
    System.out.println("objects are equal");
This code will display objects are equal even though one and anotherOne reference two different, distinct objects. They are considered equal because they contain the same integer value.

Your classes should override this method to provide an appropriate equality test. Your equals method should compare the contents of the objects to see if they are functionally equal and return true if they are.

The getClass Method

The getClass method is a final method (cannot be overridden) that returns a runtime representation of the class of this object. This method returns a Class object. You can query the Class object for a variety of information about the class, such as its name, its superclass, and the names of the interfaces that it implements. The following method gets and displays the class name of an object:
void PrintClassName(Object obj) {
    System.out.println("The Object's class is " + obj.getClass().getName());
}
One handy 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. For example, you could display a String representation of the current Thread like this:
System.out.println(Thread.currentThread().toString());
The String representation for an object is entirely dependent on the object. The String representation of an Integer object is the integer value displayed as text. The String representation of a Thread object contains various attributes about the thread, such as its name and priority. For example, the previous of code above display the following:
Thread[main,5,main]
The toString method is very useful for debugging and it would behoove you to override this method in all your classes.

Object Methods Covered In Other Lessons or Sections

The Object class provides a method, finalize that cleans up an object before its garbage collected. This method's role during garbage collection is discussed in this lesson in Cleaning Up Unused Objects. Also, Writing a finalize Method shows you how to write override the finalize method to handle the finalization needs for you classes.

The Object class also provides five methods that are critical when writing multithreaded Java programs:

  • notify
  • notifyAll
  • wait (three versions)
These methods help you ensure that your threads are synchronized and are covered in Threads of Control
Which of these method of object class is used to obtain class of an object at run time?
. Take particular note of the page titled Synchronizing Threads.

Which of this method of object class is used to obtain a class of an object?

Object. getClass() If an instance of an object is available, then the simplest way to get its Class is to invoke Object. getClass() .

Which method of an object class can be used to return class type in Java?

getClass() Method: It's return runtime class object and used to get metadata information as well.

Which of these method of object class can call on an object?

2. Which of these method of Object class can clone an object? Explanation: None.

Which are the methods of the object class?

Methods of Object class.