Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I come from a C++ background where the rules are a bit different from Java, but I think these two-stage initialization principles apply in the general case.</p> <p><strong>Construction</strong></p> <ol> <li>Everything that <em>can</em> be done at construction time <em>should</em> be done at construction time.</li> <li>Minimize the amount of "badness" that can result from trying to use your object before calling <code>init()</code>.</li> <li>All member variables need a value, even if it's a normally invalid sentry value (e.g., set pointers to null). I think Java will initialize all variables to zero by default, so you need to pick something else if that's a valid number.</li> </ol> <p><strong>Initialization</strong></p> <ol> <li>Initialize those member variables that depend on the existence of other objects. Basically, do the things you couldn't do at construction time.</li> <li>Ensure your object is now in a complete, ready-to-use state. Consider throwing an exception if it isn't.</li> </ol> <p><strong>Reset</strong></p> <ol> <li>Think long and hard about what state The System will be in when you'd want to call such a function. It may be better to create a new object from scratch, even if that operation seems expensive. Profile your code to find out if that's a problem.</li> <li>Assuming you got past item 1, consider writing a method to handle the things that both <code>reset()</code> and your constructor need to do. This eases maintenance and avoids code duplication.</li> <li>Return your object to the same state it was in after <code>init()</code>.</li> </ol>
    singulars
    1. This table or related slice is empty.
    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. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    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