title: How can I implement abstract static methods in Java?
body: <p>There are numerous questions about the impossibility of including static abstract Java methods.  There are also quite a lot about workarounds for this (design flaw/design strength).  But I can't find any for the specific problem I'm going to state shortly.  </p>
  
  <p>It seems to me that the people who made Java, and quite a lot of the people who use it, don't think of static methods the way I, and many others, do - as class functions, or methods that belong to the class and not to any object.  So is there some other way of implementing a class function?  </p>
  
  <p>Here is my example: in mathematics, a <strong>group</strong> is a set of objects that can be composed with each other using some operation * in some sensible way - for example, the positive real numbers form a group under normal multiplication (<em>x</em> * <em>y</em> = <em>x</em> × <em>y</em>), and the set of integers form a group, where the 'multiplication' operation is is addition (<em>m</em> * <em>n</em> = <em>m</em> + <em>n</em>).  </p>
  
  <p>A natural way to model this in Java is to define an interface (or an abstract class) for groups:</p>
  
  <pre><code>public interface GroupElement
  {
    /**
    /* Composes with a new group element.
    /* @param elementToComposeWith - the new group element to compose with.
    /* @return The composition of the two elements.
     */
    public GroupElement compose(GroupElement elementToComposeWith)
  }
  </code></pre>
  
  <p>We can implement this interface for the two examples I gave above: </p>
  
  <pre><code>public class PosReal implements GroupElement
  {
    private double value;
  
    // getter and setter for this field
  
    public PosReal(double value)
    {
      setValue(value);
    }
  
    @Override
    public PosReal compose(PosReal multiplier)
    {
      return new PosReal(value * multiplier.getValue());
    }
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>public class GInteger implements GroupElement
  {
    private int value;
  
    // getter and setter for this field
  
    public GInteger(double value)
    {
      setValue(value);
    }
  
    @Override
    public GInteger compose(GInteger addend)
    {
      return new GInteger(value + addend.getValue());
    }
  }
  </code></pre>
  
  <p>However, there's one other important property that a group has: every group has an <strong>identity element</strong> - an element <em>e</em> such that <em>x</em> * <em>e</em> = <em>x</em> for all <em>x</em> in the group.  For example, the identity element for positive reals under multiplication is <em>1</em>, and the identity element for integers under addition is <em>0</em>.  In that case, it makes sense to have a method for each implementing class like the following: </p>
  
  <pre><code>public PosReal getIdentity()
  {
    return new PosReal(1);
  }
  
  public GInteger getIdentity()
  {
    return new GInteger(0);
  }
  </code></pre>
  
  <p>But here we run into problems - the method <code>getIdentity</code> doesn't depend on any instance of the object, and should therefore be declared <code>static</code> (indeed, we may wish to refer to it from a static context).  But if we put the <code>getIdentity</code> method into the interface then we can't declare it <code>static</code> in the interface, so it can't be <code>static</code> in any implementing class.  </p>
  
  <p>Is there any way of implementing this <code>getIdentity</code> method that: </p>
  
  <ol>
  <li>Forces consistency over all implementations of <code>GroupElement</code>, so that every implementation of <code>GroupElement</code> is forced to include a <code>getIdentity</code> function.</li>
  <li>Behaves statically; i.e., we can get the identity element for a given implementation of <code>GroupElement</code> without instantiating an object for that implementation.  </li>
  </ol>
  
  <p>Condition (1) is essentially saying 'is abstract' and condition (2) is saying 'is static', and I know that <code>static</code> and <code>abstract</code> are incompatible in Java.  So are there some related concepts in the language that can be used to do this?</p>