views:

147

answers:

3

I have an assignment that is requiring me to use the factory pattern for implementing an immutable data structure, but the problem is that the abstract class is generic, and having static methods make references to generic types is giving me problems. My assignment is requiring me to use static methods so I'm starting to panic. Any help/suggestions? EDIT added some sample code, and here is the specification for one of the methods the professor gave us Signature:

ExampleClass.method1 : ExampleClass, T -> ExampleClass

ExampleClass.method2 : ExampleClass - > T

public abstract class ExampleClass<T>{

   //static method creates a new subclass of Example ("Push" method)
   public static Class method1(T x, ExampleClass c){
       return new method1(x, f);
    }   

   //Supposed to return an object type T ("pop" method)
   public static T method2(ExampleClass c){
       return c.method2Dynamic();
   }

both of the methods I have like these are giving me problems in eclipse.

A: 

See the Java tutorial for help with generic methods (as opposed to generic classes).

From your description thus far, the static method vs. instance method question shouldn't come into play here.

Michael Brewer-Davis
+1  A: 

I don't know what you actually want to do, but let's suppose the problem is you are just looking for the right syntax:

public class ExampleClass<T> {
    public static <T> Class<T> method1(T x, ExampleClass<T> c) {
        return c.method3(x);
    }
    public static <T> T method2(ExampleClass<T> c) {
        return c.method2Dynamic();
    }
    private Class<T> method3(T x) {
        return null;
    }
    private T method2Dynamic() {
        return null;
    }
}
Thomas Mueller
A: 

If you have a generified class, you can't use the type parameters in static methods since they don't make sense there. For example, consider ArrayList<T>. You can create a ArrayList<String> stingList = new ArrayList<String>() and a ArrayList<Integer> integerList = new ArrayList<Integer>. So now you have to instances of ArrayList, each with their own parameter type, and instance methods that can take advantage of that parameter type like get. But static methods belong to the class not the instance, so when you call a static method you call it like ArrayList.staticMethod() NOT stringList.staticMethod() or integerList.staticMethod() (you can do that as well, but it doesn't really make sense, since the static method cannot access instance variables, so it does the exact same thing as calling it on the class). But when you call it on the class, the class is just ArrayList, without any type parameters, since the type parameters are only used by instances.

You can however have methods that have their own type parameter, which is independent of the type parameter of the class, like Thomas shows in his answer. So you can then call those method like ExampleClass.<String> staticMethod(); notice that ExampleClass has no type parameter here, but the method does have it. (you can omit the <String> in the method call if the compiler can infer it from the parameters used, or the return type: String s = ExampleClass.method2(new ExampleSubclass<String>()); it usually does a pretty good job at inferring it)

Andrei Fierbinteanu