We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
In previous articles, we discussed about singleton design pattern and singleton class implementation in detail.
In this article, we will see how we can create singleton classes. After reading this article you will be able to create your singleton class according to your use, simplicity and removed bottlenecks.
There are many ways this can be done in Java. All these ways differs in their implementation of the pattern, but in the end, they all achieve the same end result of a single instance.
Eager initialization: This is the simplest method of creating a singleton class. In this, object of class is created when it is loaded to the memory by JVM. It is done by assigning the reference an instance directly.
It can be used when program will always use instance of this class, or the cost of creating the instance is not too large in terms of resources and time.
// Java code to create singleton class by
// Eager Initialization
public class GFG
{
// public instance initialized when loading the class
private static final GFG instance = new GFG();
May lead to resource wastage. Because instance of class is created always, whether it is required or not.
CPU time is also wasted in creation of instance if it is not required.
Exception handling is not possible.
Using static block: This is also a sub part of Eager initialization. The only difference is object is created in a static block so that we can have access on its creation, like exception handling. In this way also, object is created at the time of class loading.
It can be used when there is a chance of exceptions in creating object with eager initialization.
// Java code to create singleton class
// Using Static block
public class GFG
{
// public instance
public static GFG instance;
private GFG()
{
// private constructor
}
{
// static block to initialize instance
instance = new GFG();
}
}
Pros:
Very simple to implement.
No need to implement getInstance() method. Instance can be accessed directly.
Exceptions can be handled in static block.
Java Singleton Pattern
You are viewing a single comment's thread. Return to all comments →
In previous articles, we discussed about singleton design pattern and singleton class implementation in detail. In this article, we will see how we can create singleton classes. After reading this article you will be able to create your singleton class according to your use, simplicity and removed bottlenecks. There are many ways this can be done in Java. All these ways differs in their implementation of the pattern, but in the end, they all achieve the same end result of a single instance.
Eager initialization: This is the simplest method of creating a singleton class. In this, object of class is created when it is loaded to the memory by JVM. It is done by assigning the reference an instance directly. It can be used when program will always use instance of this class, or the cost of creating the instance is not too large in terms of resources and time. // Java code to create singleton class by
// Eager Initialization public class GFG
{ // public instance initialized when loading the class private static final GFG instance = new GFG();
private GFG() { // private constructor } public static GFG getInstance(){ return instance; } } Pros:
Very simple to implement. Cons:
May lead to resource wastage. Because instance of class is created always, whether it is required or not. CPU time is also wasted in creation of instance if it is not required. Exception handling is not possible. Using static block: This is also a sub part of Eager initialization. The only difference is object is created in a static block so that we can have access on its creation, like exception handling. In this way also, object is created at the time of class loading. It can be used when there is a chance of exceptions in creating object with eager initialization.
// Java code to create singleton class // Using Static block public class GFG
{ // public instance public static GFG instance;
private GFG()
{ // private constructor }
{ // static block to initialize instance instance = new GFG(); } } Pros:
Very simple to implement. No need to implement getInstance() method. Instance can be accessed directly. Exceptions can be handled in static block.
This may help you try. https://www.cetpainfotech.com/technology/java-training