When should a static method be used? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

When should a static method be used?

I never understood why using the static method and when to use it, could anyone explain it to me?

11th Mar 2017, 4:59 AM
Kelvi Dalmazo
Kelvi Dalmazo - avatar
5 Answers
+ 1
so you can use it anywhere... u use static variables so that you can access and update them with every object.
11th Mar 2017, 5:16 AM
Michael Szczepanski
Michael Szczepanski - avatar
+ 1
 "These are called with the type name. No instance is required—this makes them slightly faster. Static methods can be public or private." The main thing is that you don't need to have an instance of an object to call your static method. Source: https://www.dotnetperls.com/static 😉
11th Mar 2017, 7:20 AM
pierakim
+ 1
A non-static method has its owner as an additional parameter. tiger.eat(kitten) Is the same as eating(tiger,kitten) But something there is no real owner. Or at most exactly one owner, like when you want to talk to the system clipboard. There is exactly one of those. So in C# standard library, the Clipboard class has only static methods.
11th Mar 2017, 10:40 AM
1of3
1of3 - avatar
+ 1
Static methods are good candidates for writing Utility functions as utilities aren't intended to maintain any state and creating objects for utilities may sound overkill. Static methods are one way to manage Singletons too. Some other use cases are to write some initialization code on app startup. Hope it helps!
12th Mar 2017, 6:02 AM
Amarkumar Bardolia
0
Thanks guys 👊
11th Mar 2017, 7:26 AM
Kelvi Dalmazo
Kelvi Dalmazo - avatar