I want to read in a datafile that has several constants for my program (e.g. MAXARRAYSIZE).
I then want these constants to be accessible anywhere in my program by typing something like: ConstantsClassName.MAXARRAYSIZE. How do I implement this class?
Once assigned from the datafile, these constants will never again change value during program execution.
Thanks.
Use a static bloc in
ConstantsClassName
class.MAXARRAYSIZE
should beMAX_ARRAY_SIZE
if you follow Java conventions for constants declaration.If their are lots of constants in your file, you can use below snippet of code:
Now use the keyValues HashMap to get the value you have for the constant like
In this way you do not have to define multiple constant variables for multiple constants, only keyValues HashMap is sufficient to store all the constants and its value. Hope it helps.