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

What does \ mean in regex?

619


What is r in java?

591


What is autoboxing and unboxing?

577


Explain the difference between private, public, package and protected in java?

594


How big is a gigabyte?

611






What restrictions are placed on method overloading in java programming?

568


How to provide security in java

1802


Why singleton class is used in java?

566


What is return code?

558


what is thread? What are the high-level thread states? Or what are the states associated in the thread? : Java thread

495


Is a copy constructor?

572


What lambda means?

541


Is 9 a prime number?

464


What is difference between arraylist and list in java?

578


How many bytes is a unicode character?

502