title: how to return the number of times Singleton accessed getInstance()?
body: <p>I want to use a private int type accessCount() to return the times of Singleton has been accessed via the getInstance() method, but always get error. Here's my two piece of code:</p>
  <p><strong>ExampleSingleton.java</strong></p>
  <pre><code>public class ExampleSingleton {
    private int accessCount;
    private static ExampleSingleton instance = new ExampleSingleton();
    private ExampleSingleton() {
      System.out.println(&quot;I, the ExampleSingleton, am being created&quot;);
     }
    public static ExampleSingleton getInstance() {
      System.out.println(&quot;The sole instance of ExampleSingleton is being retrieved&quot;)
      
    }
  
  }
  </code></pre>
  <p><strong>ExampleTest.java</strong></p>
  <pre><code>public class ExampleTest {
    public static void main(String[] args) {
      ExampleSingleton s = ExampleSingleton.getInstance();
      System.out.println(&quot;The ExampleSingleton has been &quot;
      + &quot;accessed via the getInstance() method &quot;
      + s.accessCount()
      + &quot; time(s)&quot;);
      s = ExampleSingleton.getInstance();
      System.out.println(&quot;The ExampleSingleton has been &quot;
      + &quot;accessed via the getInstance() method &quot;
      + s.accessCount()
      + &quot; time(s)&quot;);
    }
  }
  </code></pre>
  <p>So I can get the following output
  <a href="https://i.stack.imgur.com/AKjXC.png" rel="nofollow noreferrer">output</a></p>