Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Wiki Markup
Preferring static factory methods over {{public}} or {{protected}} constructors also helps limit class extensibility. \[[Bloch 08|java:AA. Java References#Bloch 08]\]. Typically, a {{private}} constructor is explicitly provided to override the default no-argument {{public}} constructor.

Code Block
bgColor#ccccff
class BankOperation {
  private BankOperation() {
    // ...
  }
  public static void factoryMethod() {
    // ...
  }
}

...

A third solution is to provide a non-empty private constructor accessible only from within the class.

Code Block
bgColor#ccccff
class BankOperation {
  private BankOperation() {
    // ...
  }
}

...