Search code examples
javastringprojectconstantsfinal

How to refer a constant value from one project to another project in java


I have a class where I have few constant values assigned and I want to refer to them in another class which belongs to other project.

Lets say i have a below class :

public class sampleConstants {

    private static final String SOME_VALUE = "ABC";
        private static final String SOME_VALUE2 = "DEF";
.
.
.
.

}

And the class "sampleConstants" belongs to project "X" and i want to refer to the constant variables from this class in anpother class which belongs to project "Y".

Is it possible ? if yes, Please let me know.


Solution

  • You have to first get clear on your own requirements: when you change one of those constants, does that mean that you should immediately recompile both projects?

    Or is it OK that you define SOME_VALUE=1 within X today, and you compile and create a JAR with that constant ... and import that within Y. And when you happen to change SOME_VALUE=2 tomorrow, your project Y can continue to work with the initial value for some time?

    Thus, the real point here is: depending on how these constants are used, the other solution can be fully okay, but it can also be the wrong approach. You see, when your two projects are tightly coupled, and changing the constant in one place makes it necessary to change it in the second, too ... then the real answer could be to carefully look at the way you defined your projects. Maybe, just maybe, it isn't a good solution to have code that needs these constants to reside in two different projects.

    Maybe the real solution would be to ensure that all code that uses these constants sits in a single project.