I was reading some code somewhere on the internet, and I saw this interesting piece that intrigued me and I'm curious how it works.
There is a class called ResourceManager and looks like this:
public class ResourceManager {
private static final ResourceManager INSTANCE = new ResourceManager();
public static ResourceManager getInstance() {
return INSTANCE;
}
}
(It has a bunch of other stuff in it but I don't think its necessary to include). However, what I find interesting is that in the author did not include a constructor. In fact, in his main method he only makes one reference to this class and instead of creating a new object he just writes:
ResourceManager.getInstance().etc();
I have never seen coding like this before. I had to modify it because I needed a ResourceManager object to work with, so what I did was:
ResourceManager res = ResourceManager.getInstance();
Which worked perfectly fine. However, I still don't entirely understand what is going on here. How is this class being created without a constructor?