Immutable classes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Zalek Bloom

    Immutable classes

    I am learning about difference between String and StringBuffer
    classes. In a book it sayes that a String class is immutable, that
    means that one an instance of String class is created, the string it
    contais cannot be changed, only reference will change to point to a
    different string. So results will be different in the following code
    depending is a, b are defined as String or StringBuffer:

    a = new String("test") (or new StringBuffer("t est")
    b = a

    a = a.any_method_th at_modifies_a

    out.println(b)

    My question: how do I know if a class is Immutable? How to define an
    Immutable class?

    Thanks,

    Zalek
  • Christophe Vanfleteren

    #2
    Re: Immutable classes

    Zalek Bloom wrote:
    [color=blue]
    > I am learning about difference between String and StringBuffer
    > classes. In a book it sayes that a String class is immutable, that
    > means that one an instance of String class is created, the string it
    > contais cannot be changed, only reference will change to point to a
    > different string. So results will be different in the following code
    > depending is a, b are defined as String or StringBuffer:
    >
    > a = new String("test") (or new StringBuffer("t est")
    > b = a
    >
    > a = a.any_method_th at_modifies_a[/color]

    The thing with Strings is: when you call a.any_method_th at_modifies_a.. ., a
    is not really modified, but instead a new String is created. So to say that
    the references changes is incorrect: you only get a reference to a new
    String object. The original String object still exists.
    [color=blue]
    >
    > out.println(b)
    >
    > My question: how do I know if a class is Immutable? How to define an
    > Immutable class?[/color]

    As you said, a class is immutable if it's contents can not be changed, which
    means that instances of the class will always be the same after they have
    been constructed.

    So if you have a class that contains a field which you can only set within
    the constructor, you have an immutable class.

    eg:

    public class Immutable {
    private Object something

    public Immutable(Objec t something){
    this.something = something;
    }
    }

    Now instances of this class can not be changed anymore, it's something field
    will always be the same.

    Now consider this:

    public class Immutable {
    private Object something

    public Immutable(Objec t something){
    this.something = something;
    }

    public void setSomething(Ob ject something) {
    this.something = something;
    }
    }

    Now you can change the something field of the instance, so the class is no
    longer immutable.
    [color=blue]
    >
    > Thanks,
    >
    > Zalek[/color]

    --
    Regards,
    Christophe Vanfleteren

    Comment

    • John

      #3
      Re: Immutable classes

      Here is a discussion of immutables :


      Zalek Bloom wrote:
      [color=blue]
      >I am learning about difference between String and StringBuffer
      >classes. In a book it sayes that a String class is immutable, that
      >means that one an instance of String class is created, the string it
      >contais cannot be changed, only reference will change to point to a
      >different string. So results will be different in the following code
      >depending is a, b are defined as String or StringBuffer:
      >
      >a = new String("test") (or new StringBuffer("t est")
      >b = a
      >
      >a = a.any_method_th at_modifies_a
      >
      >out.println( b)
      >
      >My question: how do I know if a class is Immutable? How to define an
      >Immutable class?
      >
      >Thanks,
      >
      >Zalek
      >
      >[/color]

      Comment

      Working...