title: JAXB Is there some way to unmarshal static variables?
body: <p>I have class 'Settings' which stores my app's settings in static variables (to be "visible" from anywhere in app) and I would like to have functionality of saving/loading it.</p>
  
  <p>simplified Settings class:</p>
  
  <pre><code>@XmlRootElement
  @XmlAccessorType(XmlAccessType.NONE)
  public class Settings {
      @XmlElement
      private static int option = 0;
  
      private Settings() {
      }
  
      public static int getOption() {
          return option;
      }
      public static void setOption(int option) {
          Settings.option = option;
      }
  }
  </code></pre>
  
  <p>Code used to marshal:</p>
  
  <pre><code>public static void main(String[] args) throws JAXBException {
      JAXBContext context = JAXBContext.newInstance(Settings.class);
      Marshaller m = context.createMarshaller();
      m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
      m.marshal(new Settings(), new File("c:\\test\\test.xml"));
  }
  </code></pre>
  
  <p>And output xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
  &lt;settings&gt;
      &lt;option&gt;**0**&lt;/option&gt;
  &lt;/settings&gt;
  </code></pre>
  
  <p><strong>Now the problem:</strong> when I change value of <em>static int option</em> by calling Settings.setOption(5); as shown below and do unmarshal of the previously marshaled option (which was 0), the resulting Settings object has <strong>value of Settings.option same as the current Settings.option</strong>, which is 5.</p>
  
  <pre><code>Settings.setOption(5);
  JAXBContext context = JAXBContext.newInstance(Settings.class);
  Settings s2 = (Settings)context.createUnmarshaller().unmarshal(new File("c:\\test\\test.xml"));
  // Settings.option is 5, but should be 0!
  </code></pre>
  
  <p>I just hoped that after unmarshalling it would actually set all static variables of Setting to match with new created object "by nature", but it seems not.<br />
  Is there any way to achieve such behavior while preserving static variables? Or am I completely wrong about method of doing save/load of app settings? Please, help :)</p>