0

What will happen when I call two Class.getInstance twice in the same method?

For example:

private void widgetListeners() {
        notifyNews.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                if (notifyNews.isChecked()) {
                    FirebaseMessaging.getInstance().subscribeToTopic(getString(R.string.news_subscriber));
                } else {
                    FirebaseMessaging.getInstance().unsubscribeFromTopic(getString(R.string.news_subscriber));
                }
                return true;
            }
        });
        notifyVersion.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                if (notifyVersion.isChecked()) {
                    FirebaseMessaging.getInstance().unsubscribeFromTopic("Version");
                } else {
                    FirebaseMessaging.getInstance().unsubscribeFromTopic(Version"));
                }
                return true;
            }
        });
    }

Would it better if I call FirebaseMessaging.getInstance() once at the top of the widgetListeners() method body and store it in a variable or I just call in eachonPreferenceChange or I stick to what I've already done above?

Note: I call widgetListeners() in onCreateView of a fragment that extends the PreferenceFragment class.

Cœur
  • 37,241
  • 25
  • 195
  • 267
X09
  • 3,827
  • 10
  • 47
  • 92
  • 3
    `getInstance` is something you implement, so it will behave however you want it to. Could it be that you're confusing it with the method `newInstance()` ? – Nir Alfasi Oct 06 '16 at 22:34
  • Possible duplicate of [Difference between calling new and getInstance()](http://stackoverflow.com/questions/3170159/difference-between-calling-new-and-getinstance) – Nir Alfasi Oct 06 '16 at 22:34
  • As @alfasin said, it depends on the implementation of `getInstance()`, e.g. [`Calendar.getInstance()`](https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html#getInstance--) will always return a *new* instance, while [`LayoutStyle.getInstance()`](https://docs.oracle.com/javase/8/docs/api/javax/swing/LayoutStyle.html#getInstance--) always returns the same shared instance. – Andreas Oct 06 '16 at 22:43
  • There is no such method as `Class.getInstance()`. – user207421 Oct 06 '16 at 23:37
  • 1
    @EJP ofcourse I know. I'm only using it as an example. No need for the downvote. – X09 Oct 06 '16 at 23:42

1 Answers1

1

getInstance() is usually an artifact of the singleton pattern. From a high level, that means you only ever access one instance of this class, which is created the first time you access it.

What you've already done should work well. The instance is already stored for you, so there's really no need to store it in another reference in your case.

wuoix
  • 38
  • 4
  • 1
    Most `getInstance()` methods in the Java Runtime Library actually returns new instances, e.g. [`Calendar.getInstance()`](https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html#getInstance--). – Andreas Oct 06 '16 at 22:44
  • @Andreas oh, interesting. I did not realize that, thanks for comment. – wuoix Oct 06 '16 at 22:47
  • Thanks. Please I need a little clarification. What happens when you create it the second time? – X09 Oct 06 '16 at 22:48
  • @Ozuf there's not enough [documentation](https://firebase.google.com/docs/reference/android/com/google/firebase/messaging/FirebaseMessaging) on `FirebaseMessaging` publicly for this answer to be completely resolved. It seems like there would be no need for `FirebaseMessaging` to return a new object as the `Calendar` class does. If you would really like to play it safe, you can instantiate it on the top of widgetListeners() as you suggest. – wuoix Oct 06 '16 at 23:06
  • 1
    @wuoix `getInstance` is used in the factory pattern, singleton is an instance (pun intended) of the factory pattern. A factory can control the number of generated instances (such as in the Singleton case or when implementing object pooling) but it can also create a new instance upon every invocation. Depends on the case, – Nir Alfasi Oct 06 '16 at 23:09
  • If that's the correct [source code](https://github.com/FabianTerhorst/Screener/blob/master/java/com/google/firebase/messaging/FirebaseMessaging.java) you could add the link to the answer from which it's seen that in case of `FirebaseMessaging` it's a singleton so each call to `getInstance()` returns a reference to the same object. – Onik Oct 06 '16 at 23:26