This question already has an answer here:
Singleton:
public class Test{
private Test singleInstance = new Test();
private int someVariable;
private Test(){
}
public Test getInstance(){
return this.singleInstance;
}
public void someMethod(){
//do nothing
}
}
Using static on fields/methods:
public class Test{
public static int someVariable;
private Test(){
}
public static void someMethod(){
//do nothing
}
}
Having static on all the fields/methods will make the class behave like one instance of an object. Is there any reason for using Singleton instead?
Aucun commentaire:
Enregistrer un commentaire