Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>First off, kudos to you for your choice in Java-lit: Bloch's book is an <em>excellent</em> primer.</p> <p>To answer your 2nd question ('unlike constructors static factory methods are not required to create a new object each time they are invoked'), it's important to realize that what Bloch is saying here is that with a static factory you have the <em>option</em> of either: returning a new object or returning a pre-existing one. It all depends on what you want to do.</p> <p>For example, let's suppose you have a really simple value class of type Money. Your static factory method probably should return a new instance -- that is, a new object with a specific value for Money. So, like this:</p> <pre><code>public class Money { private Money(String amount) { ... } /* Note the 'private'-constructor */ public static Money newInstance(String amount) { return new Money(amount); } } </code></pre> <p>But let's say you have some object that manages some resource and you want to synchronize access to that resource through some ResourceManager class. In that case you probably want your static factory method to return the same instance of itself to everyone -- forcing everyone to go through that same instance, so that that 1 instance can control the process. This follows the singleton-pattern. Something like this:</p> <pre><code>public ResourceManager { private final static ResourceManager me = new ResourceManager(); private ResourceManager() { ... } /* Note the 'private'-constructor */ public static ResourceManager getSingleton() { return ResourceManager.me; } } </code></pre> <p>The above method forces your user to only ever be able to use a single instance, allowing you to precisely control who(and when) has access to whatever it is you are managing.</p> <hr> <p>To answer your first question, consider this (admittedly not the best example, it's pretty ad-hoc):</p> <pre><code>public class Money { private Money(String amount) { ... } public static Money getLocalizedMoney( MoneyType localizedMoneyType, String amount ) { switch( localizedMoneyType ) { case MoneyType.US: return new Money_US( amount ); case MoneyType.BR: return new Money_BR( amount ); default: return new Money_US( amount ); } } } public class Money_US extends Money { ... } public class Money_BR extends Money { ... } </code></pre> <p>Note how I can now do this:</p> <pre><code>Money money = Money.getLocalizedMoney( user_selected_money_type ); saveLocalizedMoney( money ); </code></pre> <p>Again, a really contrived-example but hopefully it helps you see more or less what Bloch was getting at with that point.</p> <p>The other answers were good -- I just think that, as a beginner, sometimes it helps to see some actual code.</p>
 

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