thieumao

Singleton in Java

Apr 6th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.51 KB | None | 0 0
  1. package singleton;
  2.  
  3. class Printer {
  4.    
  5.     private static Printer instance;
  6.    
  7.     private Printer() {}
  8.    
  9.     public static Printer getInstance() {
  10.         if (instance == null) {
  11.             instance = new Printer();
  12.         }
  13.         return instance;
  14.     }
  15.    
  16.     public void connect() {
  17.         System.out.println("Do something here");
  18.     }
  19.    
  20. }
  21.  
  22. public class Singleton {
  23.    
  24.     public static void main(String[] args) {
  25.         Printer.getInstance().connect();
  26.     }
  27.    
  28. }
Advertisement
Add Comment
Please, Sign In to add comment