Communiquez avec les autres et partagez vos connaissances professionnelles

Inscrivez-vous ou connectez-vous pour rejoindre votre communauté professionnelle.

Suivre

Is it better to have a single instance of a class, or 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?

user-image
Question ajoutée par Usama Saad , Senior Software Engineer (Java) , Saudi Catering Company
Date de publication: 2013/08/23
Vivek Dudani
par Vivek Dudani , Software Engineer / Senior Software Engineer , Mastercard

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.

anas alhashki
par anas alhashki , Senior Full Stack Engineer , Industrial Motor Power Corporation

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.

Shadi Al Barhouch
par Shadi Al Barhouch , Developer , THALES GROUP

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.

Huzefa chikhly
par Huzefa chikhly , JAVA DEVELOPER , PRIMALINK SOLUTIONS INDIA PVT LTD

if class in function orianted means a class which is made to purform some functionality like Math class in java then its better to use bunch of static methods. but if class is data orianted means it holds some data but that same data is use throughout the application then its batter to use singleton pattern so that a single instance of tht class can be use for entire application.

خالد السيد
par خالد السيد , Maintenance Engineer , confidential

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.

Mufeed Farhan
par Mufeed Farhan , HR Applications Manager , SBM

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.

Muhammad Haseeb
par Muhammad Haseeb , Senior Software Developer , Stella Technology

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.

More Questions Like This