title: Avoid duplicate Strings in Java
body: <p>I want to ask a question about avoiding String duplicates in Java.</p>
  
  <p>The <em>context</em> is: an XML with tags and attributes like this one:</p>
  
  <pre><code>&lt;product id="PROD" name="My Product"...&gt;&lt;/product&gt;
  </code></pre>
  
  <p>With JibX, this XML is marshalled/unmarshalled in a class like this:</p>
  
  <pre><code>public class Product{
  private String id;
  private String name;
  // constructor, getters, setters, methods  and so on
  }
  </code></pre>
  
  <p>The program is a long-time batch processing, so Product objects are created, used, copied, etc.</p>
  
  <p>Well, the <em>question</em> is:
  When I analysed the execution with software like <em>Eclipse memory analyzer (MAT)</em>, I found several duplicated Strings. For example, in the id attribute, the <em>PROD</em> value is duplicated around 2000 instances, etc.</p>
  
  <p>How can I avoid this situation? Other attributes in Product class may change their value along the execution, but attrs like <em>id</em>, <em>name</em>... don't change so frequently.</p>
  
  <p>I have readed something about <em>String.intern()</em> method, but I haven't used yet and I'm not sure it's a solution for this. Could I define the most frequent values in those attributes like <em>static final</em> constants in the class?</p>
  
  <p>I hope I'd have expressed my question in a right way.
  Any help or advice is very appreciated. Thanks in advance.</p>