1

I am wondering how would I ensure that when an instance has been created that it is then impossible to change whatever is being passed through that instance. Therefore data can only can be created once.

I have a class with get and set methods of an accountNo and SortCode, how would I ensure that when this instance is called within a main method, then it can't be changed afterwords.

Thanks

apapapaa
  • 27
  • 6

2 Answers2

2

Make the fields final.

final int accountNo;
LLL
  • 3,199
  • 2
  • 22
  • 39
0

you need a strategy defining your objects Immutable

how:

  • declare members as final

  • don't provide setters

  • don't let subclass to override methods

check the Immutable RGB example here

ΦXocę 웃 Пepeúpa ツ
  • 45,713
  • 17
  • 64
  • 91