Let me say there is an abstract class which looks like
abstract class Parent<V> {
protected static <T extends Parent<V>, V> T newInstance(
final Class<T> type, final V value) {
// ...
}
}
Within following 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? newInstance1
or newInstancw2
?
It actually depends on the scenario in which you are you using the newInstance()
. In most general cases:
Since Child
is implementing newInstance()
, According to me
protected static Child newInstance()
{
// ...
}
would be more appropriate.
Usually, factory method defined inside some class returns instance of this particular class, so it should be:
public class Foo ...
{
public static Foo newInstance ()
{
...
}
}
regardless of what class this class extends and what interfaces it implements.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With