So, to sum up.
That pattern is called Static Factory Method and it's described for example here: How to use "Static factory methods" instead of constructors?
In simplest form it looks like this:
class A {
public static A newA() {
return new A();
}
private A(){}
}
Your example is little bit more complex as it includes computing parameters for invoking constructor
public class A {
private A(int param1, String param2) {}
public static A createFromCursor(Cursor c) {
// calculate param1 and param2 from cursor
return new A(param1, param2);
}
}
The purpose of using such way for creating new object may be the need to repeat those calculating every time before calling new A(params)
directly. So it's just avoiding repeating yourself, and simplest way to achieve this is creating a method.
Furthermore using the same way, you can provide even more options to create new A
. For example you can change the way your calculation works with:
public static A createFromCursorDifferently(Cursor c) {
// calculate param1 and param2 from cursor in different way
return new A(param1, param2);
}
Then you may pass the same parameter to this method, and result will be different (coz method name is varies).
And of course you can create your new A
from any other parameter, using the same constructor as before:
public static A createFromObject(Object o) {
// calculate param1 and param2 from object
return new A(param1, param2);
}
So you have much more possibilities with Static Factory Methods than with simple usage of constructors only.