Pregunta de entrevista de Cognizant

1 what are immutable class in java ? how you make immutable claas/

Respuesta de la entrevista

Anónimo

21 nov 2009

1)The Class should be final 2)The Properties of the class should be private and should not have any setter methods The above mentioned are bare minimum conditions that needs to be satisfied to make the object immutable class ImmutableCart { private final List items; public ImmutableCart(List items) { this.items = Collections.unmodifiableList(new ArrayList(items)); } public List getItems() { return items; } public int total() { /* return sum of the prices */ } }

1