Based on Benoit's thoughts of what you want to achive, I think the best way is using a Map (or ConcurrentMap if you want thread-safety) :
ConcurrentMap<String, List<String>> myData = new ConcurrentHashMap<>();
This way you can address any list by the id you provided.
List<String> myList = myData.get(id);
If you want to restrict the list's accessors (e.g. only provide the add method) you need to encapsulate the list in a class :
public final class Example {
private final List<String> docs = new ArrayList<>();
public boolean addDoc(final String doc) {
return docs.add(doc);
}
}
And then use the Map as follows :
ConcurrentMap<String, Example> myData = new ConcurrentHashMap<>();
And add docs like that :
myData.get(id).addDoc(myDoc);
Hope this helps...
On the topic discussed in comments : setting variables
You have a class like this :
public class Example {
public String var;
}
And an instance like this
Example ex = new Example();
You can set the value with
ex.var = "abc";
With a calss like this
public class Example {
private String var;
public void setVar(String var) {
this.var = var;
}
}
use
ex.setVar("abc");
Managing multiple instances :
1) Your web-service get the information with an id
2) Your server-application stores a Map of instances and you can access it through the ID (see Map example above).
In the web-Service you call
Example ex = ReportHolder.getReport(id);
Assuming a class like this :
public class ReportHolder {
private static ConcurrentMap<String, Example> map = new ConcurrentMap<>();
public static Example getReport(final String id) {
return map.get(id);
}
}
3) Then you can manipulate the instance.
Make sure you understand the terms variable, class, instance and static correctly. Otherwiese it will be hard to understand why your error happened.
myExamplevar? Can't you makemyMethodreturn it? That way, any external point will manage its own instance as it needs.myExamplehas local visibility in the methodmyMethod, it is not visible at all to external call. Or do you plan to havemyExampleas instance or class variable ?myMethodwhy don't you just call the constructor to create a new object. I see no benefit in using this method, and btw. - as mentioned obove - the created Example object cannot be used outside the method.