public class RecordManager {
public static final RecordManager INSTANCE = new RecordManager();
private RecordManager() {
// private constructor prevents instantiation
}
}
Your JAX-WS service or JAX-RS resource would reference RecordManager like this:
@GET
public ... (...){
...
rec_man = RecordManager.INSTANCE;
rec_man.update( <parameters passed by call> )
}
Or, if you don't like that style (singleton as a public static instance) you could hide the static instance and expose a static method to obtain it.
public class RecordManager {
private static RecordManager instance;
private RecordManager() {
// private constructor prevents instantiation
}
public static RecordManager getInstance() {
if (instance == null) {
instance = new RecordManager();
... init
}
return instance;
}
}
Your usage becomes:
@GET
public ... (...){
...
rec_man = RecordManager.getInstance();
rec_man.update( <parameters passed by call> )
}
Note that if your instantiation logic needs to be threadsafe (e.g. only ever initialize once) then you could make the getInstance
method synchronized
or one of the techniques described in this article.