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)
======================================================================