Singleton classes

Most of interviewers asking the question to java and android developers. It's not strange. We need to explore this.

Singleton:

  •  Create the only one instance in the class.
  •  Provide a global point of access to the object
  •  Allow multiple instances in the future without affecting a singleton class's client

In android applications, It's help to hold the global session data. It's easily to call from another class.

Imagine, If you creating the MyApplication class, that's need to be access in all over the application means.

private static MyApplication sInstance;

public static MyApplication getInstance(){
      return sInstance;
}

public void onStart(){
---
}

public void onTerminate(){
--
}

public void onPlay(){
--
}

If you want to access onStart(), onTerminate() or OnPlay() methods from another classes means, you need to use singleton.

So buddies, I hope you clear now. If you not clear, please practice. :)

Comments

Popular posts from this blog

Flutter Bloc - Clean Architecture

What's new in android 14?

Dependencies vs Dev Dependencies in Flutter