immutability - java + Should I make the instace variable class immutable -


from http://docs.oracle.com/javase/tutorial/essential/concurrency/imstrat.html :

don't provide "setter" methods — methods modify fields or objects referred fields. make fields final , private. don't allow subclasses override methods. simplest way declare class final. more sophisticated approach make constructor private , construct instances in factory methods. if instance fields include references mutable objects, don't allow objects changed: don't provide methods modify mutable objects. don't share references mutable objects. never store references external, mutable objects passed constructor; if necessary, create copies, , store references copies. similarly, create copies of internal mutable objects when necessary avoid returning originals in methods.

a class called employee has instance variable a of type address
question : should done make class immutable ( condition: want return same instance variable a getaddress() getter).
should make address class immutable ?

note: understand defensive copy, not want use this

you can make address of employee final variable, has instantiated before constructor finishes execution. can modify constructor of employee takes address param n inits instance address variable. once set u can't assign new address obj init address variable.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -