JDK-5046180 : Class interface support to have interface definition of the static methods
  • Type: Enhancement
  • Component: specification
  • Sub-Component: language
  • Affected Version: 1.4.2
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: linux
  • CPU: x86
  • Submitted: 2004-05-12
  • Updated: 2006-11-06
  • Resolved: 2006-11-06
Related Reports
Duplicate :  
Description
Name: rmT116609			Date: 05/12/2004


A DESCRIPTION OF THE REQUEST :
Class interface support

Java Developers right developers invariably face a problem of not able get benefit interfacing the classes that have to define static methods for access or can't even enforce classes to have interface definition of the static methods. Interestingly they have to define an interface for non-static part and an abstract class to enforce static part to the subclasses.

Example:

public interface NonStaticPartOfASingleton {
	// non-static methods
}


public abstract class AbstractSingleton implements NonStaticPartOfASingleton {
    private static NonStaticPartOfASingleton instance;
    private AbstractSingleton () {
    }
    public static NonStaticPartOfASingleton getInstance() {
        if(instance == null) {
 synchronized {
                     createInstance();
	}
        }
        return instance;
    }
     
    private static void createInstance() {
	return null;
    }
}

public class Singleton extends AbstractSingleton {
	// implemetaion
       private static void createInstance() {
	return new Singleton()
        }
}


Now comes the tricky part; what if I need to extend the behavior of another class? I��������ll end up making a �������Adapter������� and a �������Decorator������� to �������NonStaticPartOfASingleton������� interface. I know after having Class Interface we��������ll still have to do this sometimes but key thing is we can avoid 50% of the time. 

Example if we has class interface:
public interface NonStaticPartOfASingleton {
	// non-static methods
}

public classinterface SingletonClass {
     public static NonStaticPartOfASingleton  getInstance();
}

public class Singleton implements NonStaticPartOfASingleton, SingletonClass {
	// implemetaion
       private static void createInstance() {
	return new Singleton()
        }
}

Now you can extend any other if need be.

This will get us in the new era of;Class Level; inheritance.


EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Expected behavior of classinterface:

1.Should be able to cast Class Objects to classinterface. SingletonClass singletonClass = (SingletonClass) Singleton.class; (Will help in making �������Like class������� factories) 
2. Should be able to cast Objects to it��������s classinterface. 
3. Should be able to use instanceof operator for classinterface. 


CUSTOMER SUBMITTED WORKAROUND :
I assume well have to make some changes to the class loading mechanisms and class loaders to, with added key word addition to the compiler's lexical. I hope we may also be able to support �������public abstract static void doThisMyChild();������� like function. 
(Incident Review ID: 232232) 
======================================================================

Comments
EVALUATION I believe the core request here is for static methods in interfaces.
06-11-2006