This question already has an answer here:

I regularly write classes which can only have immutable instances, much like string.

I am wondering why Java or C# or VB.NET don't have immutability built-in into the language? That way, I can communicate immutability better to my fellow programmers and also make assumptions about state in the class.

For instance: immutable class Foo { public string Name { get; set; } //compile error: mutable property on immutable class }

marked as duplicate by gnat, user22815, Doc Brown, user40980, Bart van Ingen Schenau Jan 25 '15 at 11:57

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
  • Strings in Java are immutable – sakisk Jan 24 '15 at 19:32
  • @faif Implicitly so (as far as the language), since it is carefully written so that (1) none of the code in the class itself modifies an instance's value and (2) all state is properly hidden and not aliased, so that (non-malicious) outside of the class can't modify it either. What Ruudjah is getting at is some language feature that automated or checks or otherwise supports these properties. – user7043 Jan 24 '15 at 19:35
  • 1
    C++ has const, not exactly what you want, but near. – Basile Starynkevitch Jan 24 '15 at 20:29
  • 1
    What? Java has the final keyword. If an object's state is all final, and the classes (e.g. string, primitives, wrappers) that comprise its members all have final state, then your object is very likely immutable. That is built-in to the language. – user22815 Jan 24 '15 at 20:37

C# does have immutability built in. So does Java, and VB.NET.

Mutable:

public class Counter
{
   private int count;

   public Counter(int initialCount)
   {
       count = initialCount;
   }

   public void Increment()
   {
       count++;
   }
}

Immutable:

public class Counter
{
   private readonly int count;  // `final` for Java

   public Counter(int finalCount)
   {
       // set at construction time, but readonly, so can't modify.
       count = finalCount;
   }

   public void Increment()
   {
       // Invalid operation
       count++;
   }
}

Note that I'm using "immutability" hear to mean "set once during object construction." Constants are not very useful in this context, because they can only be set at compile-time.

If you dig deep enough, every programming language mutates state under the hood, because that's how the processor instructions are designed.

  • 3
    I think what the OP was actually thinking about was a way to declare a whole class as immutable, not just individual attributes of that class. – Philipp Jan 24 '15 at 21:09
  • "If you dig deep enough, every programming language mutates state under the hood, because that's how the processor instructions are designed.": If a programming language does not allow mutation, the fact that its implementation does use mutation is irrelevant for the programmer. – Giorgio Jan 24 '15 at 21:10
  • I guess what the OP probably meant was "why do mainstream OO languages don't have immutability (or immutable variables) by default." (like Haskell). – Doc Brown Jan 24 '15 at 21:12
  • 1
    @Philipp: What do you mean by whole class? Are you referring to the possibility to change a class at runtime, e.g. adding new methods? – Giorgio Jan 24 '15 at 21:12
  • 1
    @Giorgio No, I was thinking about a keyword in the class definition which results in the class no longer being allowed to have non-final / non-readonly attributes (not that I believe that this would be a good idea). – Philipp Jan 24 '15 at 21:25

Not the answer you're looking for? Browse other questions tagged or ask your own question.