I was reading on this software engineering page about the use of static methods. However, I'm confused, and maybe it's about the context in which it talks about static. The idea is, that static is really bad for testing and shouldn't be used.
When creating immutable class, sometimes the constructor is private and a static factory is used to create the object. Even Java's own documentation supports the use of a factory when creating immutable objects.
A more sophisticated approach is to make the constructor private and construct instances in factory methods
Usually the factory is static, example:
private Weapon(String name, int damage)
{
this.name = name;
this.damage = damage;
}
public void attack(Enemy enemy){ // code to cause damage to enemy }
public static Weapon Sword(String name, damage){
return new Weapon(name, damage);
}
public static Weapon Sniper(String name, damage){
return new Weapon(name, damage);
}
In the context of creating an immutable object when the constructor is private, is it okay to use static factory, or just make the constructor public?