Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Beginners Object Class Methods in Java

 
 

Share on Google+Share on Google+

Object Class Methods in Java

Advertisement
We are going to discus about Object Class Methods in Java. The java.lang.Object class is the root of the class hierarchy tree in JDE(java development environment). Every Java class extends the java.lang.Object class directly or indirectly. There are many methods defined in java.lang.Object class these are as clone(), equals(), finalize(), notify(), notifyAll(), getClass(), toString(), wait(), etc

We are going to discus about Object Class Methods in Java. The java.lang.Object class is the root of the class hierarchy tree in JDE(java development environment). Every Java class extends the java.lang.Object class directly or indirectly. There are many methods defined in java.lang.Object class these are as clone(), equals(), finalize(), notify(), notifyAll(), getClass(), toString(), wait(), etc

Java object class methods are:-

  • finalize()
  • clone()
  • equals()
  • notify()
  • notifyAll()
  • getClass()
  • toString()
  • wait()

1. finalize() Method:-

finalize() method of java.lang.Object class may be overridden into the subclasses for arranging the system resources or to do another cleanup. The finalize method is called before garbage collected, the runtime system call its finalize() method. Object's class finalize method is declared as protected, it doesn't operate special action. finalize method can be invoked by any thread but, the thread responsible for invoking this method would not be keep the any user-visible synchronization locks at the time of invoking of finalize(). The garbage collector calls the finalize method of Object class before reclaiming the memory.

Syntax:-

  protected void finalize() throws Throwable {
           super.finalize();
        }

2. clone() Method:-

 We have created a clone().method of this java.lang.Object and returns copy of this object. clone() method as the name suggest clones the object of the same class and assigns the same value as the original object. It clones those object whose class displays that its instance is willing to be cloned by declaring it in Cloneable interface.

Syntax:-

y.clone()!=y //there will be true, and that the expression
y.clone().getClass()==y.getClass()//there will be true, but these are not absolute requirements. while it is typically the case that.
y.clone.equals(y)// there will be true, it is not an absolute requirement

3. equals()Method:-

The equals().Method are compare two objects equals. The equals method return true when object are equal,otherwise return false. The equals method found in java.land.Objecwe have use to equals method and compare two Object for equality and we use equals method when an object is store in a hashtable.

How to compare two objects equality are:-

   Integer one = new Integer(2), otherInteger = new Integer(2);

        if (one.equals(anotherInteger))
        System.out.println("objects are equals");

4. notify() Method:-

The notify() Method has a single waiting thread on a change in condition of another thread.We can call only synchronized method.This method only called by a thread and thread that owns objects monitor is the only one that must call this method. notify() Method of the object class java.lang.Object.

How to declare notify method are:-

public final void notify()

5. notifyAll() Method:-

 The notifyAll method has all of the threads waiting for a condition to change.This method only call by within a synchronized method. The notify() method of the object class java.lang.Object. The threads has waiting are generally   waiting for another thread to some condition.We can use for notify() method.

public final void notifyAll()

6.getClass() Method:-

We are going to discus about getClass() Method. The getClass method has final method cannot be overridden. These method are return a class object.

How to use getClass() method are:-

      void PrintClassName(Object obj) {
      System.out.println("The Object's class is " + obj.getClass().getName());
      }

7. toString() Method:-

We are going to discus about toString Method in java. The toString method returns a string representation of the object. The toString() method returns a string that is in a format which is easy to understand for the user. toString() method returns a string name of the class of which the object is an instance.

Syntax:-

 String toString()
static String toString(int i)

8. wait() Method:-

Wait() Method is a defined inside the object class java.lang.Object The wait method is hold the thread  resign the lock till thread the object. The wait method to be notify by different thread of a modify in this object. The wait method can call by synchronized block. The Current thread to pending another thread invoke the notify() method this object.

wait() Method are three type defined:-

  1. public final void wait()
                throws InterruptedException

 2. public final void wait(long timeout)
                throws InterruptedException
 
 3.public final void wait(long timeout,
                       int nanos)
                throws InterruptedException
Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: May 18, 2013

Related Tutorials

Discuss: Object Class Methods in Java  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com