title: Static field with program code
body: <p>I have learned that in a class you can declare variables and methods. They can both be declared as static if that is prefered.</p>
  
  <p>Now I have encountered a program example I do not really understand. After some variable declaration in the class there is a field declared as static and inside there is program code.</p>
  
  <p>When is this code executed? My guess is that the code is executed like following when a new object is created:</p>
  
  <ol>
  <li>Memory allocation for the instance variables.</li>
  <li>Execution of the contructors connected to the instance variables.</li>
  <li>Exection of the static field</li>
  <li>Execution of the constructor (if "= new Constructor() is used when the object is created)</li>
  </ol>
  
  <p>If I execute</p>
  
  <blockquote>
  <pre><code>MyCars myCars = new MyCars();
  </code></pre>
  </blockquote>
  
  <p>the following will happen in this order?</p>
  
  <blockquote>
  <pre><code>public class MyCars
  {
      private Car volvo = new Car()
   //              (1)       (2)
      static
      {
          volvo.setNumberOfWheels = 4;
   //                  (3)
      }
  
      public MyCars()
      {
           volvo.setBrand = "Volvo";
           volvo.setModel = "XC70";
                   (4)
      }
  </code></pre>
  </blockquote>
  
  <p>Here is the original code:</p>
  
  <blockquote>
  <pre><code>public class SettingsSetter extends ListActivity
  {
      private static Map&lt;Integer,String&gt; menuActivities=new HashMap&lt;Integer,String&gt;();
      private static List&lt;BooleanSetting&gt; settings=new ArrayList&lt;BooleanSetting&gt;();
  
      static 
      {
          menuActivities.put(R.id.app,
              Settings.ACTION_APPLICATION_SETTINGS);
          menuActivities.put(R.id.security, Settings.ACTION_SECURITY_SETTINGS);
          menuActivities.put(R.id.wireless,
              Settings.ACTION_WIRELESS_SETTINGS);
          menuActivities.put(R.id.all,
              Settings.ACTION_SETTINGS);
  
          settings.add(new BooleanSetting(Settings.System.INSTALL_NON_MARKET_APPS,
              "Allow non-Market app installs", true));
          settings.add(new BooleanSetting(Settings.System.HAPTIC_FEEDBACK_ENABLED,
              "Use haptic feedback", false));
          settings.add(new BooleanSetting(Settings.System.ACCELEROMETER_ROTATION,
              "Rotate based on accelerometer", false));
      }
  </code></pre>
  </blockquote>