JDK-4291381 : Add static methods to interfaces
  • Type: Enhancement
  • Component: specification
  • Sub-Component: language
  • Affected Version: 1.2.2
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: generic
  • CPU: generic
  • Submitted: 1999-11-15
  • Updated: 1999-11-16
  • Resolved: 1999-11-16
Related Reports
Duplicate :  
Description

Name: mc57594			Date: 11/15/99


java version "1.2.2"
Classic VM (build JDK-1.2.2-W, native threads, symcjit)

Much of the software I develop extensively uses reflection for the loading of
classes at runtime.  These classes often implement an interface which I use as
the "contract" to decide whether the class in suitable for loading (it is
easier to check if it implements an interface rather than if it implements all
the required methods).  To make this less vague, here is an example.

I have a logging system which loads the required logging classes at runtime
based on user/sys admin choices.  These logging classes must have a constructor
with a specific signature, a number of instance methods and a number of static
methods.  Here's the problem:  You cannot specify constructor signatures, nor
static methods in an interface.

The second issue is a stumper.  The static method are required to tell a
configuration utility what properties the logging object needs specified, so I
want to make this static, and not have to construct an object only to find out
what information it requires!!  If I specify the static methods as non-static
within the interface, then they can't be implemented as static within any
derived classes.  Nor can I use static final constants for this particular
task.
(Review ID: 97779) 
======================================================================

Comments
WORK AROUND Name: mc57594 Date: 11/15/99 The work around to the first problem is obvious - use an default constructor to build the logging object, then call an init(...) method with the required parameters. Still, I believe it would be nice to specify the constructor signature for use with Reflection. The workaround to the static method is to make each class have a static member variable, a default (empty) constructor, and an instance method which returns the static member. Again I just feel it would be easier done through use of an interface. It would also mean that programmers implementing logging objects for my system would know at compile-time whether their objects conformed to the required contract. Maybe the case I'm arguing for is a Reflection Interface which is a special case of an Interface and allows these type of features. ======================================================================
11-06-2004

PUBLIC COMMENTS See bug 4093687. Basically, adding static methods to interfaces is an idea worth considering.
10-06-2004