I was always doubtful over two terms in Java (not limited to, though!), so they get a quote respect here:
final, and immutable
If one goes by English literature, both of them are twins or at least best friends. In Java, there's considerable difference in their usage, although their meanings are stunningly similar.
• immutable variables
• immutable methods,
and neither
• final objects.
Take my words! I've seen, though,
- final classes
- final variables
- final methods
and
- immutable objects.
Consider it, its damn serious, if you still need diapers in Java!
When we declare something "final", this means that we have finalized how should it be. So, thereon, it can not be redefined, reinitialized, or extended, whatsoever! So once we are done with finalizing variables, methods or classes, we can not go for a re-touch, without modifying the same piece of source code.
Immutability is described, in Java, in terms of objects. Once you make any object immutable, it can not be altered, or modified. The state of immutable objects are consistent and final. Oops, I just said final.
There's no keyword as such as "immutable" in Java. We have to provide a way to make the objects immutable.
► How can we do so? Ponder please!
What points came to your mind?
Lets protect the object. That's it!
I will
- make all fields private and final. This ensures consistency of object's state, and protection too.
- make the class final so that it is not extended and the class's logic is not redefined.
- provide no setter methods that can modify objects or fields.
- provide no way to modify the mutable objects if any field points to an external mutable object. That's because, if that external object's state is somehow changed, then our immutable object remains no longer immutable.
[Note: This article will be expanded or edited suitably in future]
• immutable methods,
and neither
• final objects.
Take my words! I've seen, though,
- final classes
- final variables
- final methods
and
- immutable objects.
Consider it, its damn serious, if you still need diapers in Java!
When we declare something "final", this means that we have finalized how should it be. So, thereon, it can not be redefined, reinitialized, or extended, whatsoever! So once we are done with finalizing variables, methods or classes, we can not go for a re-touch, without modifying the same piece of source code.
Immutability is described, in Java, in terms of objects. Once you make any object immutable, it can not be altered, or modified. The state of immutable objects are consistent and final. Oops, I just said final.
There's no keyword as such as "immutable" in Java. We have to provide a way to make the objects immutable.
► How can we do so? Ponder please!
What points came to your mind?
Lets protect the object. That's it!
I will
- make all fields private and final. This ensures consistency of object's state, and protection too.
- make the class final so that it is not extended and the class's logic is not redefined.
- provide no setter methods that can modify objects or fields.
- provide no way to modify the mutable objects if any field points to an external mutable object. That's because, if that external object's state is somehow changed, then our immutable object remains no longer immutable.
[Note: This article will be expanded or edited suitably in future]
No comments:
Post a Comment
Liked or hated the post? Leave your words of wisdom! Thank you :)