November 2018
Intermediate to advanced
388 pages
9h 5m
English
Let's understand how to write a singleton class in Java. We then compare it with the singleton class in Kotlin.
Let's consider the following class:
public class Singleton { public String getDetails() { System.out.println ("running the method"); return "this is a singleton class"; }}
If we want to instantiate this class, we do it as follows:
new Singleton ();
Calling a constructor like this gives an instance of this class. If we repeatedly call this constructor, we get multiple instances of the same class and it is therefore not a singleton pattern.
Now we want only one instance of this class to be created and reused. To create a singleton class, we need to do the following:
Read now
Unlock full access