+ 1

When do you use constants?

I don't know the usage of constants

19th Feb 2018, 11:12 AM
Jag
2 Answers
+ 2
i agree that it doesn't have daily uses but there is a use for it after all .... when you have a bunch of values that wouldn't change ..... like days of week . points for tasks ......
19th Feb 2018, 11:17 AM
Farshaad Heydari
Farshaad Heydari - avatar
+ 1
You might want a particular class to be initialized with some values but then be unable to change. Using final in Java means you can't change it (with some caveats) so that enforces that behavior. If you come back to it after some time or another developer works on your code this also communicates that intention. You could imagine you made a booking for a holiday - you could allow customers to amend a booking or cancel and make a new booking. Sometimes the latter might be preferable
19th Feb 2018, 3:25 PM
Dan Walker
Dan Walker - avatar