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,566
  • 2
  • 25
  • 44
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 ツ
  • 47,427
  • 17
  • 69
  • 97