What is singleton class and how can we make a class singleton?
Answer / Km Srishti Tripathi
A Singleton class is a design pattern that ensures a class has only one instance, while providing a global point of access to it. In Java, we can achieve singleton behavior using various methods such as the Hungry, Eager, and Lazy Initialization patterns. Here's an example of the Eager Initialization pattern:
```java
public class SingletonExample {
private static final SingletonExample instance = new SingletonExample();
private SingletonExample() {}
public static SingletonExample getInstance() {
return instance;
}
}
```
In the example above, the instance is created when the class is loaded rather than being created on demand. This ensures that only one instance of the `SingletonExample` exists and provides a global access point to it through the `getInstance()` method.
| Is This Answer Correct ? | 0 Yes | 0 No |
What are the advantages of unicode?
Can we clone singleton object?
What do you mean by object?
Is empty in java?
what is the difference b/w design pattern and architecture
Explain differences between checked and unchecked exceptions in java?
What are different types of classloaders?
What are the library functions in java?
What is prefix of a string?
Difference between this() and super() in java ?
What defines function?
How do you sort data in java?