user1714575 user1714575 - 1 year ago 109
Java Question

Read in file for constants #java

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.


Answer Source

Use a static bloc in ConstantsClassName class.

public class ConstantsClassName{
    public static final  String MAXARRAYSIZE;
        // read your file and store the data in;
        MAXARRAYSIZE = valueRetrievedFromFile;

MAXARRAYSIZE should be MAX_ARRAY_SIZE if you follow Java conventions for constants declaration.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download