Click here to Skip to main content
15,898,924 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
when something should be implemented by a class with static operations and when by one or more objects from the same class
Posted
Updated 29-Mar-10 12:52pm
v3

The following are a bunch of the facts about static methods.

Static methods cannot be in interfaces, which can be a big deal.

They also cannot be abstract or virtual.

A static Class can only have static methods.

Static methods are faster though I have found the difference hard to measure.

Static constructors are called before regular constructors, which can be quite useful.

All extension Methods are static . They are very nice in that they let you add methods to classes that you have little control over. In particular you can add them to an enum which normally gets no methods at all. Note that you can do this to the class itself, without inheritance.

They are used in the creation of Dependency properties. DependencyProperty.Register is a good one.

My advice is as follows. When the above considerations make sense you are ready to start using static methods. Until then there are probably other more pressing issues.

Ps: To me the more interesting question is how one can best leverage static variables and collections, but that was not the question asked.
 
Share this answer
 
use static keyword where u want to call a method directly without using the instance of the class,

the keyword static is used to make fields & methods which belong to the class rather than to its instance

example

C#
class B
{
static void fun()
{
System.out.println("Hello");
}
public static void main(String args [])
{
fun();
}
}


Thanks & Regards
Radix :rose:
 
Share this answer
 
Well, any method that you may want to call where you don't have an object handy, and which has no state, should be static. If it can be static, then consider making it static.
 
Share this answer
 
When the method is directly related to a class but doesn't act on an instance of it (aside from creating a new instance of it).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900