Are all final class immutable?

匆匆过客 提交于 2019-12-09 14:29:13

问题


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


回答1:


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; }
}



回答2:


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

final class MutInt {
   public int modifyMe;
}



回答3:


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 be changed".




回答4:


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);
}



回答5:


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



回答6:


The private field can't be accessed by sub class's overridden methods...So there is no way the sub class methods change the private field of super class...Then what is the use of making the immutable class as final?




回答7:


Final keywords prevents the other classes to inherit it. Final keyword doesn't make it immutable but there are such condition as making the instance class members private as well final and by using getters and not using setters




回答8:


A final immutable classes cannot be mutated. Here below it shows a non-final immutable class being mutated:

// a class not intended to be mutated 
public class GoodClass{

   private String name;

   public GoodClass() {
       this.name = "Good Class Neme";
   }

   public String getName() {
       return name;
   }
}

public class BadClass extends GoodClass {

   private String name;

   public String getName() {
       return name;
   }

   // mutating state
   public void setName(String name) {
       this.name = name;
   }
}





来源:https://stackoverflow.com/questions/1630321/are-all-final-class-immutable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!