0

I'm basically a beginner in Java and I am trying to figure out this problem:

my project uses (integer)constant through many classes and I need to set up this constant from file/argument of programm and I have no idea how. I could remove "final" statement but that is against all conventions.

How to resolve this? What is best way to avoid it? Please help me :)

short example:

public class App {
    public static final int k;  
    public static void main( String[] args ) {
        k = Integer.parseInt(args[0]); // does not work ... sure but how? 
    }
}

EDIT: public STATIC void main ... (it was missing static)

4 Answers 4

2

You can only use this kind of functionality within the static { } block.

I'd recommend making the constant itself private, and accessible only through public static getter methods. This should be a suitable architecture.

2
  • I suppose this could work for me well, but isn't it violation of constants? Feb 1, 2014 at 0:53
  • True, however I believe that this is sufficient protection for a value that is specified via the command line.
    – Mapsy
    Feb 1, 2014 at 1:04
2

You need to use a static{} block:

public class App {
    public static final int k;

    static {
        String kvalue = "";
        try
        {
            // Insert some code to open a file and read a value from it.
            kvalue = "<value from file>";
        }
        catch( Exception e )
        {
            // handle any exceptions opening the file
        }
        finally
        {
            k = Integer.parseInt( kvalue );
        }
    }

    public static void main( final String[] args ) {
        // do stuff
    }
}
1
  • This is pretty good for files, but how about program arguments? Could it be modified somehow? Feb 1, 2014 at 0:56
1

Alternatively, you could have each class that requires this value access to a shared pool of data, the ApplicationContext, where the value X would reside as a constant:

public final class ApplicationContext {

    public static abstract class Worker {

        private final ApplicationContext mApplicationContext;

        public Worker(final ApplicationContext pApplicationContext) {
            this.mApplicationContext = pApplicationContext;
        }


        public final ApplicationContext getApplicationContext() {
            return this.mApplicationContext;
        }

    }

    private final int mX;

    ApplicationContext(final String[] pArgs) {
        this.mX = pArgs[0];
    }


    public final int getX() {
        return this.mX();
    }

}

By having all relevant classes extend ApplicationContext.Worker, we can ensure access to the constant value without having to rely upon a static implementation because all classes will receive a reference to ApplicationContext upon construction.

0
0

It doesn't work because static final variables can only be initialized in static blocks.

4
  • Could you read from a file and initialize the variable in a static block? Feb 1, 2014 at 0:37
  • 1
    Sure. You could either create a method that will read and initialize the static field, which will get invoked in the static block. Or you could place the code in a try-catch block directly in the static block.
    – Josh M
    Feb 1, 2014 at 0:38
  • I am sorry, I forgot add static statement before main method. I am talking about "public static void main" ofc Feb 1, 2014 at 0:39
  • Ok by static block you mean something like: static { k = Integer.parseInt(new SomeReader("InputFile.txt).readLine()); } ? Feb 1, 2014 at 0:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.