General <w770 method>

Let me say that there is an abstract class that looks like

abstract class Parent<V> {

    protected static <T extends Parent<V>, V> T newInstance(
        final Class<T> type, final V value) {
        // ...
    }
}

Inside the next child class

class Child extends Parent<XXX> {

    public static Child newInstance1(final XXX value) {
        // ...
    }

    public static Parent<XXX> newInstance2(final XXX value) {
        // ...
    }
}

Which one is preferable? newInstance1or newInstancw2?

+5
source share
2 answers

In fact, it depends on the scenario in which you are using newInstance(). In most cases:

Since it Childimplements newInstance(), for me

protected static Child newInstance() 
{
    // ...
}

will be more appropriate.

+4
source

Typically, a factory method defined inside a class returns an instance of that particular class, so it should be:

public class Foo ...
{
    public static Foo newInstance ()
    {
        ...
    }
}

no matter which class extends this class and which interfaces it implements.

+1

All Articles