Minhaj kaimkhani wrote:Immutable objects are objects that do not change state?? are you agree on this??
You are right Minhaj, but what you said in your previous post is not correct
Minhaj kaimkhani wrote:make the class final make fields private and final
Implement Singleton Pattern.
To be immutable, a class doesn't need to implement singleton pattern (in fact that actually doesn't has anything to do with immutable classes) and marking fields final isn't necessary either...
Yes Ankit. Singleton Pattern is like one Instance per class. In Singleton you create a Private Constructor, Declare a Static variable of same Class type and provide a Static get method which will create/or point to existing instance of the class. One can access instance of class by Static call to getter method.
But please can some one explain for Immutable if we declare class Final and fields Private and Final makes a class immutable???
Final means no subclass possible means no one can extend that class.
Rajiv Chopra wrote:But please can some one explain for Immutable if we declare class Final and fields Private and Final makes a class immutable???
Fields of an immutable class don't need to be final, there should be no setter methods for the fields, if they only have getter methods and are private, then they cannot be changed once the instance of the class is created. The thread pointed out by seetharaman explains everything very nicely...