Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In <a href="http://rads.stackoverflow.com/amzn/click/0321356683" rel="noreferrer">Effective Java, 2nd edition</a>, Joshua Bloch certainly recommends the former. There are a few reasons I can remember, and doubtless some I can't:</p> <ul> <li>You can give the method a meaningful name. If you've got two ways of constructing an instance both of which take an int, but have different meanings for that int, using a normal method makes the calling code much more readable.</li> <li>A corollary of the first - you can have different factory methods with the same parameter list</li> <li>You can return null for "potentially expected failure" cases whereas a constructor will <em>always</em> either return a value or throw an exception</li> <li>You can return a type other than the declared (e.g. return a derived class)</li> <li>You can use it as a factory, to potentially return a reference to the same object several times</li> </ul> <p>The downsides:</p> <ul> <li>It's not as idiomatic, currently - developers are more used to seeing "new"</li> <li>If you see "new" you <em>know</em> you're getting a new instance (modulo the <a href="https://stackoverflow.com/questions/194484/whats-the-strangest-corner-case-youve-seen-in-c-or-net">oddity I mentioned recently</a>)</li> <li>You need to make appropriate constructors available for subclasses</li> <li>In C# 3, constructor calls are able to set fields/properties in a compact manner with object initializer expressions; the feature doesn't apply to static method calls</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload