How to implement Singleton

Answer Posted / manoj kumar

public class DateUtil implements Serializable,Cloneable {

   private static volatile Dateutil instance;

   private DateUtil() {
     //no-op
   }

   public static DateUtil getInstance() {

      if(instance==null) {
        synchronized(DateUtil.this) {
           if(instance==null) {
              instance = new DateUtil();
           }
        }
        return instance;
      }
   
   protected Object readResolve() {
     return instance;
   }

   public Object clone() throws CloneNotSupportedException {

     super.clone();

     throw new CloneNotSupportedException;
  }

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Explain working of call by reference function invoking.

594


What is java life cycle?

541


How is the marker interface used in Java?

614


how to run ecllipse with jettyserver for windows environment using batch file

1502


Why are the destructors for base class and derived class called in reverse order when the program exits

1712






What is assembly used for?

508


What is a void method?

525


What is the full meaning of java?

542


What is the purpose of garbage collection in java?

658


what do you mean by classloader?

557


What does index mean in java?

544


Can a string be null?

547


How do you escape sequences in java?

599


What is a parameter in matrices?

547


Do we have pointers in java?

538