2

Having some issues with genericity. So, here is a simple version of what I'm dealing with:

I have one abstract class and two subclasses:

public abstract class A {}
public class B extends A {}
public class C extends A {}

I'm writing writers for those classes, and I want to keep the same architecture, because all those have a lot in common. But I want to be able to call the writer without instantiating it

public abstract class AWriter<T extends A> {
  public void AWritingMethod(T arg) {}
}
public class BWriter extends AWriter<B> {
  public static void BWritingMethod(B arg) {
    AWritingMethod(arg)
  }
}
public class CWriter extends AWriter<C> {
  public static void CWritingMethod(C arg) {
    AWritingMethod(arg)
  }
}

Obviously, I can't call AWritingMethod in BWriter and CWriter, but how could I do something like that to keep most of the work in AWriter, while still keeping BWritingMethod and CWritingMethod static ?

Thanks already !

LD

ldebroux
  • 147
  • 1
  • 9
  • That doesn't make sense at all. `AWriter.AWritingMethod` is an instance method that you want to call without creating an instance... Obviously this is impossible. – fabian Jul 20 '16 at 07:49

2 Answers2

3

You can change your code as following, add static to AWritingMethod:

public abstract class AWriter<T extends A> {
  public static void AWritingMethod(T arg) {}
}

public class BWriter extends AWriter<B> {
  public static void BWritingMethod(B arg) {
    AWriter.AWritingMethod(arg)
  }
}

public class CWriter extends AWriter<C> {
  public static void CWritingMethod(C arg) {
    AWriter.AWritingMethod(arg)
  }
}

or you can try using new instance of BWriter and CWriter class like this:

public abstract class AWriter<T extends A> {
  public void AWritingMethod(T arg) {}
}

public class BWriter extends AWriter<B> {
  public static void BWritingMethod(B arg) {
    new BWriter().AWritingMethod(arg);
  }
}

public class CWriter extends AWriter<C> {
  public static void CWritingMethod(C arg) {
    new CWriter().AWritingMethod(arg);
  }
}
Huaying
  • 151
  • 2
  • 11
Cenxui
  • 1,343
  • 1
  • 17
  • 25
  • Tried it, still getting "Non-static method 'AWritingMethod (T)' cannot be referenced from a static context" – ldebroux Jul 20 '16 at 07:45
  • Ifixed public static void AWritingMethod(T arg) {} to static method . I want to know what situation you want to get. If you want to add none static method in static method you need to new it as a instance. or make the method static to. – Cenxui Jul 20 '16 at 07:52
  • Making AWritingMethod static does not work with the generic type. The second option works though, thanks !!! – ldebroux Jul 20 '16 at 07:59
0

If you want to abstract the logic out into a separate class/method, your best bet would be to do something like this:

class AWriter {
  private AWriter() {}
  static <T extends A> void AWritingMethod(T arg) {}
}

public class BWriter {
  private BWriter() {}
  public static <T extends B> void BWritingMethod(T arg) {
    AWriter.AWritingMethod(arg)
  }
}

public class CWriter {
  private CWriter() {}
  public static <T extends C> void CWritingMethod(T arg) {
    AWriter.AWritingMethod(arg)
  }
}

Note that AWriter is not public, which is fine if BWriter and CWriter are in the same package, plus private constructors to prevent instantiation of static-only classes. This requires a bit more boilerplate around adding generics to each static method, but you don't have a lot of choice - you cannot use a classes generic types in a static context.

Community
  • 1
  • 1
ipsi
  • 2,049
  • 18
  • 23