Friday, January 10, 2025
HomeProgrammingSingleton design pattern in Java

Singleton design pattern in Java

The Singleton Design Pattern in Java ensures that a class has only one instance and provides a global point of access to it. It is used to control object creation, ensuring only one instance exists throughout the application.

See also  CSS Border

Key Features:

  1. Private Constructor: Prevents instantiation from other classes.
  2. Static Instance: Holds the single instance of the class.
  3. Public Accessor Method: Provides a global way to get the instance.

Example:

java
public class Singleton {
private static Singleton instance;

private Singleton() {} // Private constructor

public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}

Usage:

Used in logging, configuration management, database connections, and caching.

RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x