Started reading "Effective java" and can't understand why it doesn't work for me when I try coding an example..
Compile error:
Error:(12, 16) java: constructor Car in class Car cannot be applied to given types;
public class Car {
String model;
//no private constructor
public static Car fromModel(String model) {
return new Car(model);
}
}
Here everything is OK:
public class Car {
String model;
//no private constructor
public static Car fromModel(String model) {
return new Car(model);
}
}
//Here everything is OK:
public class Car {
String model;
private Car(String model) {
this.model = model;
}
public static Car fromModel(String model) {
return new Car(model);
}
}
Why should I generate constructor if "Consider static factory methods instead of constructors" ???