3

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?

MagnusCaligo
  • 707
  • 2
  • 8
  • 28
  • How does `class MyClass {}` then `MyClass myObject = new MyClass();` work, even though "MyClass doesn't have a constructor"? – user253751 Jan 04 '15 at 03:00
  • @immibis when you don't specify *any* constructor, Java creates a default one (no args) for you. If you specify one, it has to be used (i.e. no default-no args constructor). – Matthieu Jan 04 '15 at 04:05
  • @Matthieu *I* know that. I wanted to see if *MagnusCaligo* (the question author) knows that. – user253751 Jan 04 '15 at 04:06

3 Answers3

8

This is the singleton pattern, meaning that there will only ever be one instance of the class ResourceManager (the constructor should really be private to enforce this).

However, I still don't entirely understand what is going on here. How is this class being created without a constructor?

If you don't explicitly write a constructor, Java will automatically insert a default constructor (assuming the superclass also has a default constructor).

Community
  • 1
  • 1
August
  • 12,410
  • 3
  • 35
  • 51
  • Alright, so after reading what all of you have said that makes a lot of sense. However it leaves me with another question. What is the advantage of using a singleton pattern? Would it be considered "bad coding" if you didn't do this? Its not as if someone is going to be going through your own code and changing it to make two ResourceManagers – MagnusCaligo Jan 04 '15 at 03:11
  • 2
    The purpose of a singleton pattern, as its name sort of implies, is to provide a uniform access to an instance of a class. So instead of passing around references of type `ResourceManager` in each class that need it, you just call `ResourceManager.INSTANCE`. Check Effective Java's section on Singleton, it is very good. – Wickoo Jan 04 '15 at 03:15
  • "Would it be considered "bad coding" if you didn't do this?" No, actually, some people consider Singleton as an anti-pattern. So I think moderate use of it is good, when you really need a single instance of an object in your whole program. – Wickoo Jan 04 '15 at 03:17
  • So, if any class I say ResourceManager.getInstance().etc(), it will always work because there is only one instance of ResourceManager? – MagnusCaligo Jan 04 '15 at 03:17
  • The reason that it works is not because there is only one object. It works because getInstance() is defined static. As the static method returns INSTANCE, the only instance, you will have one instance. In summary, the singleton pattern provides a static access to a single instance. – Wickoo Jan 04 '15 at 03:18
1

To answer the question about no constructor:

All java classes that have no constructor defined, have an implicit public no-args constructor.

However, there should be a private no-args constructor defined, because this class is clearly intended to be a singleton. But without a private constructor, a new instance can be created at any time.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
0

In Java a class has a default constructor assuming you don't provide a constructor. Otherwise it looks like a Singleton Pattern, from the Wikipedia link, a design pattern that restricts the instantiation of a class to one object. Again, to make it a correct Singleton you should probably add a private constructor

private ResourceManager() {
  super();
}

Otherwise someone could instantiate it.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249