I would like you guys to help me out with the following things in this program. I am a beginner in java so would appreciate any help on this:
What does this means?
private static Singleton singleton = new Singleton
Are we creating a variable with type as class. I have seen many places this practice. Can you tell me why we create variables/objects with Class name?
This "Singleton singleton " is this a variable declaration or object declaration.
2)Singleton tmp = Singleton.getInstance()
Can someone help what are we doing here? We have created another object "tmp" right? What's the statement at right side doing?
public class Singleton {
private static Singleton singleton = new Singleton();
/* A private Constructor prevents any other
* class from instantiating.
*/
private Singleton() {}
/* Static 'instance' method */
public static Singleton getInstance() {
return singleton;
}
/* Other methods protected by singleton-ness */
protected static void demoMethod() {
System.out.println("demoMethod for singleton");
}
}
File: SingletonDemo.java
public class SingletonDemo {
public static void main(String[] args) {
Singleton tmp = Singleton.getInstance( );
tmp.demoMethod( );
}
Aucun commentaire:
Enregistrer un commentaire