Suppose I have an ObjectInfo class that contains the name of the object and the type of the object as a String (I'm just preparing something to ask a question.)
class ObjectInfo {
String objectName;
String objectType;
private ObjectInfo(String objectName, String objectType) {
this.objectName = objectName;
this.objectType = objectType;
}
}
And if I want to provide a static factory method to instantiate this class, which of the following two methods is better and why?
public static ObjectInfo newInstance(String objectName, String objectType) {
return new ObjectInfo(objectName, objectType)
}
public static ObjectInfo valueOf(String objectName, String objectType) {
return new ObjectInfo(objectName, objectType)
}
Basically, I want to ask when should we use valueOf () and when is newInstance ()? Are there any conventions among the programming community?
-Ankit
source
share