skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Sunday, October 14, 2012

Example of Java Singleton class

Posted by Raju Gupta at 8:00 AM – 0 comments
 
Java Singleton design pattern is one of the design pattern that governs the instantiation of an object in Java. This design pattern suggest that only one instance of a Singleton object is created by the JVM. This pattern is useful when exactly one object is needed to coordinate actions across the system

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class SimpleSingleton {
    private static SimpleSingleton singleInstance =  new SimpleSingleton();
  
    //Marking default constructor private
    //to avoid direct instantiation.
    private SimpleSingleton() {
    }
  
    //Get instance for class SimpleSingleton
    public static SimpleSingleton getInstance() {
  
        return singleInstance;
    }
}

Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger