When should I use static methods in a class and what are the benefits?

前端 未结 10 1413
孤独总比滥情好
孤独总比滥情好 2020-11-27 12:34

I have concept of static variables but what are the benefits of static methods in a class. I have worked on some projects but I did not make a method static. Whenever I need

10条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-27 13:20

    Static methods don't pass a "this" pointer to an object, so they can't reference non-static variables or methods, but may consequently be more efficient at runtime (fewer parameters and no overhead to create and destroy an object).

    They can be used to group cohesive methods into a single class, or to act upon objects of their class, such as in the factory pattern.

提交回复
热议问题