title: Singletons, Enums and anonymous inner classes
body: <p>As you may know, some people are declaring singletons with an Enum of 1 instance, because the JVM guarantees that there will always be a single instance with no concurrency problems to handle...</p>
  
  <p>Thus what about an Enum with multiple instances?
  Can we say something like an Enum is a kind of ordered set of singletons sharing a common interface?
  Why?</p>
  
  <pre><code>public enum EnumPriceType {
  
      WITH_TAXES {
          @Override
          public float getPrice(float input) {
              return input*1.20f;
          }
          public String getFormattedPrice(float input) {
              return input*1.20f + " €";
          }
          },
  
      WITHOUT_TAXES {
          @Override
          public float getPrice(float input) {
              return input;
          }
      },
      ;
  
      public abstract float getPrice(float input);
  
      public static void main(String[] args) {
          WITH_TAXES.getFormattedPrice(33f);
      }
  
  }
  </code></pre>
  
  <p>In this code why this doesn't work:
  WITH_TAXES.getFormattedPrice(33f);
  What is the interest of declaring a public method if it can't be called without passing through the common interface?
  I guess this is why i don't see any syntax to be able to declare an interface just for one of the instances of an Enum.</p>
  
  <hr>
  
  <p><strong>Edit:</strong></p>
  
  <p>It seems that enum instances are a special kind of anonymous classes.
  Thus i understand why you can't call that method.</p>
  
  <p>My question is kinda related to: <strong>why can't an anonymous class implement an interface</strong> (in addition to the interface it may already implement!)</p>
  
  <p>I totally understand why we CANT do that:</p>
  
  <pre><code>Vehicle veh = new Vehicle() {
      public String getName() {
          return "toto";
      }
  };
  veh.getName();
  </code></pre>
  
  <p>(getName here is not an override)</p>
  
  <p>Why i don't understand is why we can't do that with anonymous classes:</p>
  
  <pre><code>Runnable veh = new Vehicle() implements Runnable {
      @Override
      public void run() {
          System.out.println("i run!");
      }
  };
  veh.run();
  </code></pre>
  
  <p>Or something that would result in the same thing.
  Think about it: if you do not use anonymous classes you can absolutely extend the Vehicle class and then make that subclass implement any other interfaces you want...</p>
  
  <p>I'm pretty sure that if it was possible we would be able to call WITH_TAXES.getFormattedPrice(33f) in a typesafe way, since WITH_TAXES would not be a real EnumPriceType but it would but a subclass of EnumPriceType, with its own interface, and by calling WITH_TAXES.getFormattedPrice(33f) with a hardcoded WITH_TAXES, you know at compile that which EnumPriceType child you are calling.</p>
  
  <p>So my question is: are there any reasons why this is not possible? Or it just haven't be done yet? </p>