Inscrivez-vous ou connectez-vous pour rejoindre votre communauté professionnelle.
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?
Its always better to have multiple static methods since, it prevents the need of object creation, hence, saving space in heap.But the need of having Single instance of the class or multiple static methods purely depends upon the State of the class. If there are instance variables in the class, u'll definitely need to create object for accessing those else you can directly go for static methods.
The term you're looking for is singleton.Both static methods and instance methods on a singleton are okay approaches, but note that the static methods approach cannot implement an interface, so if you need to implement an interface, use a singleton.
Depends on your business:
If you need to have a util class : It is better to have static methods . However these methods should restricts only calculations (such as formatting date, calculating age, ... )
Now, for more complicated processes it is better to have static instance (Singleton design pattern) , because you can easily override the class and use special behavior in a special case.
you have to use the same instance if you will do some calculation or use class varibles if some variables will not change , because different instance hold different instance variables.
A system only needs to create one instance of a class, and that instance will be accessed throughout the program this is called ("Singleton") e.g if you want to create one class for controlling logging to the system and authorization so you dont have to create n instance from it .And then you can use static method becouse we use static method whenever we don't want or cannot create an object instance before using them. Based on your requiremnt you should bulid your design .
i think it is better to have one object.but if tne number of object are more then we should use static methods.
using static code is performant due to compile time binding. But, as some fellows already pointed out, it leaves you without any option to extend/enhance you class.
i would suggest you to use singleton pattern and use interfaces to abstract away your implementation. some where down the line you can add more features to your class and implement additional interfaces for new functionality.