Skip to content

Instantly share code, notes, and snippets.

@amrishodiq
Created October 5, 2011 17:20
Show Gist options
  • Save amrishodiq/1265067 to your computer and use it in GitHub Desktop.
Save amrishodiq/1265067 to your computer and use it in GitHub Desktop.
Java: Singleton concept implementation
package com.durianapp.tutorial;
/**
* @author amrishodiq
*/
public class SingletonClass {
// inilah variable yang menyimpan satu-satunya instance class SingletonClass
private static SingletonClass instance;
public static synchronized SingletonClass getInstance() {
// langkah ini memastikan hanya ada satu instance yang akan dibuat
if (instance == null) instance = new SingletonClass();
return instance;
}
/**
*
* Constructor class ini dibuat private untuk mencegah class ini dibuat
* instance-nya oleh class lain. private akan mencegak baris seperti
* berikut:
*
* SingletonClass x = new SingletonClass();
*
*/
private SingletonClass() {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment