Idea

  • Creator
    Topic
  • #3977232

    Static abstract methods in java?

    by RedCrafterLP ·

    Tags: 

    Now that c# finally allows the “static abstract” pattern to force subclasses to have a particular static method I was wondering if this could bw inspiring for the java dev team to do the same like they often did in the past. Of course the critical need for “static abstract methods” in c# for example in operators is not as huge in java. But face it. At some point or another we all wrote “static abstract” while prototyping a class hierarchy and sadly remembered that it can’t be done. Another stone in the way of “static abstract” in java is type erasure. An great use for “static abstract” would be in a method like this:

    public static void <T extends Bar> foo() {
    T.bar(); //<- static abstract method in Bar
    }

    But of course as I said type erasure prevents us to specialize T at runtime. But I heard that as part of a big type rework including things like C# struct like types the generics system is being reworked. I hope they add the “static abstract” patterns as part of this update.
    An practical example would be a generic List.of() method. You could decide which type of list it should create by providing the type the generic method should use.
    This would look similar to this:

    ArrayList<Foo> list = List.of(foo1,foo2,…);
    or
    var list = List.of<ArrayList>(foo1, foo2,…);

    The implementation would look like this:

    public interface List<E> {

    ….
    static abstract T <T extends List<U>, U> ofImpl(U[] elems);

    static T <T extends List<U>, U> o(U… elems) {
    return T.ofImpl(elems);
    }

    What do you think? Should they add it? Or do you consider it an anti pattern?

You are posting a reply to: Static abstract methods in java?

The posting of advertisements, profanity, or personal attacks is prohibited. Please refer to our Community FAQs for details. All submitted content is subject to our Terms of Use.

All Comments

Viewing 0 reply threads