Is it better to have a single instance of a class, or simply have a bunch
of static methods?
I've been using several methods of calling methods. More recently, I've
been using a static instance of a class, I do believe that's the proper
term for it (please correct me if I'm wrong). Which is better (or even
suggest ideas), and why?
The first way I was the simple old static methods.
static void exampleMethod1(){}
static void exampleMethod2(){}
The second way (someone said this is an improvement).
public class ExampleClass{
public static instance;
public ExampleClass(){
instance = this;
}
public static ExampleClass getInstance(){
return instance;
}
void exampleMethod1(){
//code
}
void exampleMethod2(){
//code
}
// To call the method I simply getInstance().exampleMethod1
}
No comments:
Post a Comment