13.07.2015 Views

4. Object Structures, Aliasing, and Encapsulation

4. Object Structures, Aliasing, and Encapsulation

4. Object Structures, Aliasing, and Encapsulation

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Sufficient criteria for immutability of an object X:• Instance variables of X cannot be modified aftertermination of its constructor.●X is not exposed during construction.• <strong>Object</strong>s referenced by X are immutableaccording to these criteria.• Constructors can only take immutable objects asparameters.• Methods do not depend on global variables, i.e.,- They do not access global variables.- They do not invoke methods that depend onglobal variables.• Methods do not create new objects.Remarks:• The above criteria are still difficult to check:- It is difficult to show that no modifications canoccur.- Inheritance has to be controlled.• Classes like String are (almost) immutable, althoughthey modify the state <strong>and</strong> do not satisfy the abovecriteria.• Restricted forms of immutability can be checkedby tools.13.05.13© A. Poetzsch-Heffter, TU Kaiserslautern18

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!