Make a class immutable by following these guidelines : - 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<http://www.javapractices.com/topic/Topic84.cjp> ) - do not provide any methods which can change the state of the object in any way - not justsetXXX methods, but any method which can change state
On Sun, Oct 2, 2011 at 11:25 PM, Tikeswar Mohanty < mohanty.tikes...@gmail.com> wrote: > can any body tell me how to declare a class immutable. > > -- > To post to this group, send email to > javaprogrammingwithpassion@googlegroups.com > To unsubscribe from this group, send email to > javaprogrammingwithpassion+unsubscr...@googlegroups.com > For more options, visit this group at > http://groups.google.com/group/javaprogrammingwithpassion?hl=en -- Trey Haertel -- To post to this group, send email to javaprogrammingwithpassion@googlegroups.com To unsubscribe from this group, send email to javaprogrammingwithpassion+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/javaprogrammingwithpassion?hl=en