title: How to find out if a class is immutable
body: <p>Before concretizing my question, let me provide some background: My main programming languages are C++ and Java. When working with C++ I find it important to apply const correctness, that is declaring functions like this:</p>
  
  <pre><code>A::DoSomething( const B&amp; arg ); // guarantees that arg is not modified
  A::DoSomething() const; // guarantees that the object of type A is not modified
  A::DoSomething( const B&amp; arg ) const; // both of the above
  </code></pre>
  
  <p>In fact, I often wish that <code>const</code> would be the default and objects that are modified have to be marked somehow.</p>
  
  <p>My main reasons for using <code>const</code>are:</p>
  
  <ul>
  <li>Communication with other developers: It makes code more expressive.</li>
  <li>Communication with the compiler: It helps to find issues at compile time and sometimes makes additional optimizations possible.</li>
  </ul>
  
  <p>It is well-known that Java has no <code>const</code> keyword (you can't do the above with <code>final</code>) and this fact has been discussed here before, see for example here: <a href="https://stackoverflow.com/questions/7894865/equivalent-of-constc-in-java">Equivalent of const(C++) in Java</a>.</p>
  
  <p>The Java alternative that is usually proposed is to make your classes immutable. While this is not a full replacement of <code>const</code> because it works per class and not per context in which a class is used, it would work fine for me in most cases.</p>
  
  <p><strong><em>But there is one big issue with immutable classes: Immutability is not obvious.</em></strong> To find out if a class really is immutable, as far as I know, you basically have to check the full source code. Any method could have a backdoor through which the object could be modified.</p>
  
  <p><strong><em>So is there an easier way to check immutability? Or is there any best practice to somehow mark a class as being immutable?</em></strong></p>
  
  <p><em>Note: I know that both languages provide 'evil tricks' to bypass constness or immutability: C++ has <code>const_cast</code> and Java has reflection. But for the context of my question, let's assume that these are not used.</em></p>