Generally speaking, the more I use immutable objects in Java, the more I think they are a great idea. They have many benefits of automatic thread safety so they don’t worry about cloning or copy constructors.
This made me wonder if the “fixed” keyword would go awry. Obviously, there are shortcomings with the addition of another reserved word to the language, and I doubt that this will happen primarily for the above reason, but ignoring the fact that I can not see many shortcomings.
Great care must be taken at this time to ensure that objects are immutable, and even then the javadoc quirky comment that claims to be a component object is immutable when it really cannot destroy it all. There is also an argument that even basic objects, such as a string, are not truly immutable, because they are easily distracted by reflection attacks.
If we had an immutable keyword, then the compiler could, of course, recursively check and give a guarantee on the hardware so that all instances of the class are immutable, which cannot be done at present. Especially with concurrency becoming more and more used, I personally think that it would be nice to add a keyword to this. But are there any flaws or implementation details that I miss that makes this a bad idea?
source
share