0

I would like to store a series of "Strings" in a separate class (Constants.java) for organizational purposes.

Constants.java:

public static final String qualification1 = "'Priority' = \"" + priority + "\" AND 'Status' = \"In Progress\" AND 'Assignee' = \"" + assigneeInput + "\"";
...

In my main application class I would like to call these from from above

Main.java:

// Examples to show that these variables are dynamic
String assigneeInput = soapResponseAssignee.getSOAPBody().getTextContent();
String[] priorityList = {"Low", "Medium", "High", "Critical"};

for (String priority : priorityList) {
    String qualification = RemedyConstants.qualification1;
    String qualification = RemedyConstants.qualification2;
    String qualification = RemedyConstants.qualification3;
    ...
}

The problem is that the Strings provided to my Main class tend to have variables in them, i.e. priority or assigneeInput, which are defined there. If I store the strings elsewhere, i.e. Constants.java, I will not actually be using the Strings that I want. How can I provide the strings that I want while referencing those variables in their appropriate context?

1
  • Step 1) learn about Properties files. Step 2 create a .properties file. Step 3) store strings in the .properties file using {0}...{n} placeholders. Step 4) create a utility class (like your Constants) that has a getString(String key, Object... arguments). Step 5) implement the method using Properties and MessageFormat.format. Step 6) Add a caching layer for the MessageFormat instances if required. Step 7) profit. Commented May 20, 2017 at 12:15

2 Answers 2

2

Use String format

public static final String qualification1 = "'Priority' = \"%s\" AND 'Status' = \"In Progress\" AND 'Assignee' = \"%s\"";

Then when you want to use it

String.format(qualification1, "Low", "Alice");

Or add function into your class doing the format

String getQualification1(String priority, String assignee) {
    return String.format(qualification1, priority, assignee);
}
Sign up to request clarification or add additional context in comments.

Comments

0

Quite simple. Not sure why it didn't click initially that I could treat the string as an object attribute...

Main.java:

String qualification = RemedyConstants.getQualification1(priority, assigneeInput);

Constants.java:

public static String getQualification1(String priority, String assigneeInput) {
        return "'Priority' = \"" + priority + "\" AND 'Status' = \"In Progress\" AND 'Assignee' = \"" + assigneeInput + "\"";
    }

1 Comment

You could do that, which will work. But hardcoding these kinds of strings into your code is bad. Also this will be somewhat slower than a cached MessageFormat. I would suggest that you look at my comment in more detail.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.