title: Why should I mark all fields private in immutable classes?
body: <p>From JDK documentation:</p>
  
  <blockquote>
    <p>Don't provide "setter" methods — methods that modify fields or objects
    referred to by fields. Make all fields final and private. Don't allow
    subclasses to override methods. The simplest way to do this is to
    declare the class as final. A more sophisticated approach is to make
    the constructor private and construct instances in factory methods. If
    the instance fields include references to mutable objects, don't allow
    those objects to be changed: Don't provide methods that modify the
    mutable objects. Don't share references to the mutable objects. Never
    store references to external, mutable objects passed to the
    constructor; if necessary, create copies, and store references to the
    copies. Similarly, create copies of your internal mutable objects when
    necessary to avoid returning the originals in your methods.</p>
  </blockquote>
  
  <p>example:</p>
  
  <pre><code>final public class ImmutableRGB {
  
      // Values must be between 0 and 255.
      final private int red;
      final private int green;
      final private int blue;
      final private String name;
  
      private void check(int red,
                         int green,
                         int blue) {
          if (red &lt; 0 || red &gt; 255
              || green &lt; 0 || green &gt; 255
              || blue &lt; 0 || blue &gt; 255) {
              throw new IllegalArgumentException();
          }
      }
  
      public ImmutableRGB(int red,
                          int green,
                          int blue,
                          String name) {
          check(red, green, blue);
          this.red = red;
          this.green = green;
          this.blue = blue;
          this.name = name;
      }
  
  
      public int getRGB() {
          return ((red &lt;&lt; 16) | (green &lt;&lt; 8) | blue);
      }
  
      public String getName() {
          return name;
      }
  
      public ImmutableRGB invert() {
          return new ImmutableRGB(255 - red,
                         255 - green,
                         255 - blue,
                         "Inverse of " + name);
      }
  }
  </code></pre>
  
  <p>Please, explain why should l mark fields as private?</p>