JDK-4491759 : Interfaces to support static declarations
  • Type: Enhancement
  • Component: specification
  • Sub-Component: language
  • Affected Version: 1.4.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: generic
  • CPU: generic
  • Submitted: 2001-08-14
  • Updated: 2001-08-15
  • Resolved: 2001-08-15
Related Reports
Duplicate :  
Description

Name: bsC130419			Date: 08/14/2001


java version "1.4.0-beta"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta-b65)
Java HotSpot(TM) Client VM (build 1.4.0-beta-b65, mixed mode)

I would like interfaces to support the declaration of static methods.

I am not sure on how this is supported overall theory of statics, but what I am
after is a way for interfaces to enforce the implementation of static methods.

In that way, I can test a class's isAssignableFrom() method to ascertain if a
specific interface is supported, but my primary objective is to find a way for
developers implementing a specific interface *know* that they must implement
the static method, rather than relying on them reading the appropriate
documentation.

An example of where this would be useful is where I might have a static
creation method on a class, thus:

<pre>
    public static Object createFromString( String definition )
    {
        ...
        return object;
    }
</pre>

  To offer this from multiple classes, I would like this method to be declared in
an interface:

<pre>
public interface Creatable
{
    public static Object createFromString( String definition );
}
</pre>

This also allows me to determine whether a class supports this static method
merely by testing whether it implements the interface, but moreover if a
developer implements the interface but neglects to implement the method, the
compiler will detect it.

<pre>
    Class objectClass = Class.forName( "..." );
    if( objectClass.isAssignableFrom( Creatable.class ) )
    {
        //  The static method createFromString is
        //  available on the objectClass class
        ...
    }
</pre>

Although the advantages of this over reflection may seem small, the big benefit
for me is that I can enforce that if a class implements my interface, then it
*has* to implement my static method
(Review ID: 129975) 
======================================================================

Comments
WORK AROUND Name: bsC130419 Date: 08/14/2001 Reflection can easily be used to determine whether the method is supported, but I would much rather move the test for whether it is there to the compiler when the interface implementation is used. ======================================================================
11-06-2004

PUBLIC COMMENTS See bug 4093687
10-06-2004

EVALUATION It's so rare to see a Java language RFE that actually makes sense. That said, this is a duplicate of 4093687. ###@###.### 2001-08-15
15-08-2001