tags:

views:

359

answers:

5

Are all final classes in Java immutable. String and Integer both are final classes and both are immutable i beleive.

+24  A: 

No, final means the class can not be extended. It says nothing about mutability. For example:

final class MutInt {
   public int modifyMe;
}
mlk
+10  A: 

No - a final class means you cannot inherit from it. It has nothing to do with mutability. The following class is final yet mutable:

public final class FinalMutable {
  int value;
  public void setValue(int v) { value=v; }
  public int getValue() { return value; }
}
David Rabinowitz
So in order to make the class immutable I have to scrap the setter() method out of the class and also make its instance variable (which in this case is "value") final.Am i correct..??
Gourav
Yes, you should scrap the setter and make "value" final.
mlk
To add to that, Rytmis make a good point below. If the value is a mutable object is your object really immutable? Best then make value private and change getValue to do a defensive copy.
mlk
So i believe this class is now an immutable onefinal class immutability_test{ private final int value; public immutability_test(int value) { this.value=value; } public final int getValue() { return value; } public static void main(String[] args) { System.out.println("The obtained value is "+new immutability_test(4).getValue()); }}Now i guees rytmi's point is also justified
Gourav
+5  A: 

There is no keyword for immutability, it's more like a design pattern.

EDIT:
This means, there is no keyword, that makes a class immutable. To make a class immutable, you have to protect the internals by make them final or private.

The confusing thing is this: The final keyword has different meanings when used on a class then it has when used on a field/variable. The former means "this class can not be extended". The Second means "this variable (or reference) can not me changed".

Tim Büthe
there is the "final" keyword for fields and variables -- but then, it doesn't make the object immutable, just the reference.
Rytmis
@Rytmis: Yes, right but there is no keyowrd that makes a class immutable, like the java.lang.String is. I'll edit the answer to make this clearer.
Tim Büthe
+4  A: 

Further to the other responses, if you look at the code for java.lang.String you'll see it contains a field: hash, which is mutable and is in fact computed and stored when hashCode() is called for the first time.

However, the class is still immutable: The hash field cannot be accessed directly or modified outside of the class.

Also, you may notice a common approach within the JDK is the implementation of immutable wrappers that can be used to expose an object's internal state without allowing it to be modified; e.g.

private final List<String> values;

public List<? get String> getValues() {
  return Collections.unmodifiableList(values);
}
Adamski
+4  A: 

As has been said by the others before final does not make a class imuutable in Java though it plays a part in the immutability strategy. To obtain immutability you should follow the general guidlines:

  • ensure the class cannot be overridden - make the class final, or use static factories and keep constructors private
  • make fields private and final
  • force callers to construct an object completely in a single step, instead of using a no-argument constructor combined with subsequent calls to setXXX methods (that is, avoid the Java Beans convention)
  • do not provide any methods which can change the state of the object in any way - not just setXXX methods, but any method which can change state
  • if the class has any mutable object fields, then they must be defensively copied when passed between the class and its caller
non sequitor
+ add a security manager do disable changing the accessible flag by using reflection!
Carlos Heuberger
One more thing: make sure to make defensive copies of any mutable arguments in the constructors. This is covered in Effective Java, 2nd Ed., item #15
Jeff Olson
Ahh yes I just looked it up item #15->5 but the actual point is made in item #39 for constructors and item #76 for `readObject` methods
non sequitor