Skip to main content
deleted 607 characters in body
Source Link
user286462
user286462

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?

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?

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

added 180 characters in body
Source Link
user286462
user286462

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, damagesdamage);
}

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?

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 static Weapon Sword(String name, damage){
    return new Weapon(name, damages);
}

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?

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?

Source Link
user286462
user286462

Using static to create an immutable object

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 static Weapon Sword(String name, damage){
    return new Weapon(name, damages);
}

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?