title: How to make thread safe singleton class which can accepts parameter?
body: <p>I am trying to make a class as <code>ThreadSafe Singleton</code> but somehow I am not able to understand how to make <code>ThreadSafe Singleton</code> class which can accepts parameter.</p>
  
  <p>Below is the class which I am using from this github <a href="https://gist.github.com/definitelyMVP/4984723" rel="nofollow">link</a> which I am using currently to make a connection to Zookeeper - </p>
  
  <pre><code>public class LeaderLatchExample {
  
      private CuratorFramework client;
      private String latchPath;
      private String id;
      private LeaderLatch leaderLatch;
  
      public LeaderLatchExample(String connString, String latchPath, String id) {
          client = CuratorFrameworkFactory.newClient(connString, new ExponentialBackoffRetry(1000, Integer.MAX_VALUE));
          this.id = id;
          this.latchPath = latchPath;
      }
  
      public void start() throws Exception {
          client.start();
          client.getZookeeperClient().blockUntilConnectedOrTimedOut();
          leaderLatch = new LeaderLatch(client, latchPath, id);
          leaderLatch.start();
      }
  
      public boolean isLeader() {
          return leaderLatch.hasLeadership();
      }
  
      public Participant currentLeader() throws Exception {
          return leaderLatch.getLeader();
      }
  
      public void close() throws IOException {
          leaderLatch.close();
          client.close();
      }
  
      public CuratorFramework getClient() {
          return client;
      }
  
      public String getLatchPath() {
          return latchPath;
      }
  
      public String getId() {
          return id;
      }
  
      public LeaderLatch getLeaderLatch() {
          return leaderLatch;
      }
  }
  </code></pre>
  
  <p>And this is the way I am calling the above class - </p>
  
  <pre><code>public static void main(String[] args) throws Exception {
          String latchPath = "/latch";
          String connStr = "10.12.136.235:2181";
          LeaderLatchExample node1 = new LeaderLatchExample(connStr, latchPath, "node-1"); // this I will be doing only one time at just the initialization time
          node1.start();
  
          System.out.println("now node-1 think the leader is " + node1.currentLeader());
  }
  </code></pre>
  
  <p>Now what I need is if I am calling these two below methods from any class in my program, I should be able to get an instance of it. So I am thinking to make above class as a Thread Safe Singleton so that I can access these two methods across all my java program.</p>
  
  <pre><code>isLeader()
  getClient()
  </code></pre>
  
  <p>How do I make above class as ThreadSafe singleton and then make use of <code>isLeader()</code> and <code>getClient()</code> across all my classes to see who is the leader and get the client instance.. </p>
  
  <p>I need to do this only at the initialization time and once it is done, I should be able to use <code>isLeader()</code> and <code>getClient()</code> across all my classes.. Is this possible to do?</p>
  
  <pre><code>// this line I will be doing only one time at just the initialization time
  LeaderLatchExample node1 = new LeaderLatchExample(connStr, latchPath, "node-1");
  node1.start();
  </code></pre>
  
  <p>This is more of Java question not Zookeeper stuff..</p>